|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Concurrency Limit package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Bogdan Koval' <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
namespace Bogkov\ConcurrencyLimit; |
|
14
|
|
|
|
|
15
|
|
|
use Bogkov\ConcurrencyLimit\Exception\LessThanMinimumException; |
|
16
|
|
|
use Bogkov\ConcurrencyLimit\Exception\SaveException; |
|
17
|
|
|
use Bogkov\ConcurrencyLimit\Provider\ProviderInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Handler |
|
21
|
|
|
* |
|
22
|
|
|
* @package Bogkov\ConcurrencyLimit |
|
23
|
|
|
*/ |
|
24
|
|
|
class Handler |
|
25
|
|
|
{ |
|
26
|
|
|
const MINIMUM_VALUE = 0; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ProviderInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $provider; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Handler constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param ProviderInterface $provider provider |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(ProviderInterface $provider) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->provider = $provider; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $key key |
|
45
|
|
|
* @param int $limit limit |
|
46
|
|
|
* |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
|
|
public function start(string $key, int $limit): bool |
|
50
|
|
|
{ |
|
51
|
|
|
$value = $this->provider->fetch($key) ?? static::MINIMUM_VALUE; |
|
52
|
|
|
|
|
53
|
|
|
if ($limit <= $value) { |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->save($key, ++$value); |
|
58
|
|
|
|
|
59
|
|
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $key key |
|
64
|
|
|
* |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
|
|
public function end(string $key)/*: void*/ |
|
68
|
|
|
{ |
|
69
|
|
|
$value = $this->provider->fetch($key); |
|
70
|
|
|
|
|
71
|
|
|
if (null === $value) { |
|
72
|
|
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->save($key, --$value); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $key key |
|
80
|
|
|
* @param int $value value |
|
81
|
|
|
* |
|
82
|
|
|
* @throws LessThanMinimumException |
|
83
|
|
|
* @throws SaveException |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function save(string $key, int $value)/*: void*/ |
|
88
|
|
|
{ |
|
89
|
|
|
if (static::MINIMUM_VALUE > $value) { |
|
90
|
|
|
throw new LessThanMinimumException( |
|
91
|
|
|
'Value ' . $value . ' less than minimum ' . static::MINIMUM_VALUE . ' by "' . $key . '"' |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (false === $this->provider->save($key, $value)) { |
|
96
|
|
|
throw new SaveException('Failed save concurrent limit by "' . $key . '" with value ' . $value); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |