Commit 0cea829e by Евгений

Улучшение #19452

Учет баллов при работе с АПИ
1 parent 2cc8fc1a
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Limits extends Model
{
use HasFactory;
}
<?php
namespace App\Service\Contract;
interface HeaderLimits{
function handleHeader(Array $headers);
function getDayLimit(): int;
function getCurrentLimit(): int;
function getSpentLimit(): int;
}
<?php
namespace App\Service\Contract;
interface Limits{
function current(): int;
function DayLimit(): int;
function countObjectsLimit(String $service, String $method): int;
function doRezerv(String $service, String $method, int $limit): int;
function removeRezerv(int $id): int;
function updateLimits(HeaderLimits $limits);
}
<?php
namespace App\Service;
class HeaderLimits implements \App\Service\Contract\HeaderLimits{
private $dayLimit = 0;
private $currentLimit = 0;
private $spentLimit = 0;
function handleHeader(Array $headers)
{
if (!isset($headers['Units'])){
throw new Exception('Не найден заголовок с баллами');
}
list($this->spentLimit, $this->currentLimit, $this->dayLimit) = explode("/", $headers['Units']);
}
function getDayLimit(): int
{
return $this->dayLimit;
}
function getCurrentLimit(): int
{
return $this->currentLimit;
}
function getSpentLimit(): int
{
return $this->spentLimit;
}
}
<?php
namespace App\Service;
use App\Models\Tokens;
use App\Service\Contract\HeaderLimits;
class Limits implements \App\Service\Contract\Limits {
private $_instance;
private $token;
private function __constructor(Tokens $token){
$this->token = $token;
}
public function getInstance($token){
$this->_instance = new self($token);
}
function current(): int
{
return $this->token->limit;
}
function DayLimit(): int
{
$this->token->limits->first()->day;
}
function countObjectsLimit(string $service, string $method): int
{
$cost = $this->getCost($service, $method);
return floor($this->current() / $cost);
}
function doRezerv(string $service, string $method, int $limit): int
{
// TODO: Implement doRezerv() method.
}
function removeRezerv(int $id): int
{
// TODO: Implement removeRezerv() method.
}
function updateLimits(HeaderLimits $limits)
{
// TODO: Implement updateLimits() method.
}
private function getCost(){
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLimitsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('limits', function (Blueprint $table) {
$table->id();
$table->bigInteger('token')->unsigned();
$table->string('service', 128);
$table->string('method', 128);
$table->integer('spent')->unsigned();
$table->integer('current')->unsigned();
$table->integer('day')->unsigned();
$table->boolean('reserved');
$table->timestamps();
$table->foreign('token')->references('id')->on('tokens');
});
Schema::table('tokens', function (Blueprint $table) {
$table->integer('limit');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('tokens', function (Blueprint $table) {
$table->dropForeign('limits_token_foreign');
$table->dropColumn('limit');
});
Schema::dropIfExists('limits');
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!