Limits.php
4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace App\Service;
use App\Models\Tokens;
use App\Service\Contract\HeaderLimits;
use Illuminate\Support\Facades\DB;
class Limits implements \App\Service\Contract\Limits {
private static $_instance;
private $token;
private $limitCosts;
private function __constructor(Tokens $token){
$this->token = $token;
$this->limitCosts = new Costs();
}
public static function getInstance($token): Limits
{
self::$_instance = new self($token);
}
function current(): int
{
return $this->token->limit;
}
function DayLimit(): int
{
$this->token->limits->first()->day;
}
function countObjectsLimit(\App\Service\Contract\APIRequest $request): int
{
$cost = $this->limitCosts->getCostObject($request);
if ($this->limitCosts->getCostCall($request) > $this->current()){
return 0;
}
return $cost > 0 ? floor(($this->current() - $this->limitCosts->getCostCall($request)) / $cost) : -1;
}
/**
* @param string $service
* @param string $method
* @param int $limit
* @return int
* @throws \Exception
* предполагается что класс работает в очереди.
* Иначе может быть что одновременно будет два резервирования с одним и тем же остатком.
*/
function doRezerv(\App\Service\Contract\APIRequest $request, int $objects): int
{
$limit = $this->getSpent($objects, $request);
if ($this->token->limit<$limit){
throw new \Exception('Недостаточно баллов');
}
DB::beginTransaction();
try{
$rezerv = new \App\Models\Limits();
$rezerv->token = $this->token->id;
$rezerv->service = $request->getService();
$rezerv->method = $request->getMethod();
$rezerv->spent = $limit;
$rezerv->current = $this->token->limit;
$rezerv->reserved = 1;
$rezerv->save();
$this->token->limit -= $limit;
$this->token->save();
DB::commit();
}catch(\Exception $e){
DB::rollBack();
throw $e;
}
return $rezerv->id;
}
function removeRezerv(int $id): int
{
DB::beginTransaction();
try{
$limit = \App\Models\Limits::findOrFail($id);
$this->token->limit += $limit->spent;
$this->token->save();
$limit->delete();
DB::commit();
}catch(\Exception $e){
DB::rollBack();
throw $e;
}
}
function acceptRezerv($id, HeaderLimits $limits){
DB::beginTransaction();
try{
$this->token->limit = $limits->getCurrentLimit();
$this->token->save();
$limit = \App\Models\Limits::findOrFail($id);
$limit->spent = $limits->getSpentLimit();
$limit->current = $limits->getCurrentLimit();
$limit->day = $limits->getDayLimit();
$limit->reserved = 0;
$limit->save();
DB::commit();
}catch(\Exception $e){
DB::rollBack();
throw $e;
}
}
function updateLimits(HeaderLimits $limits)
{
DB::beginTransaction();
try{
$this->token->limit = $limits->getCurrentLimit();
$this->token->save();
$limit = new \App\Models\Limits();
$limit->token = $this->token->id;
$limit->spent = $limits->getSpentLimit();
$limit->current = $limits->getCurrentLimit();
$limit->day = $limits->getDayLimit();
$limit->reserved = 0;
$limit->save();
DB::commit();
}catch(\Exception $e){
DB::rollBack();
throw $e;
}
}
private function getSpent($objects, \App\Service\Contract\APIRequest $request): int
{
$cost = $this->limitCosts->getCostObject($request);
return $objects * $cost + $this->limitCosts->getCostCall($request);
}
}