1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hamburgscleanest\GuzzleAdvancedThrottle; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
6
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters\ArrayAdapter; |
7
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters\LaravelAdapter; |
8
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Interfaces\CacheStrategy; |
9
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Interfaces\StorageInterface; |
10
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Strategies\Cache; |
11
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Strategies\ForceCache; |
12
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Strategies\NoCache; |
13
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownCacheStrategyException; |
14
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownStorageAdapterException; |
15
|
|
|
use Psr\Http\Message\RequestInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class RequestLimitRuleset |
19
|
|
|
* @package hamburgscleanest\GuzzleAdvancedThrottle |
20
|
|
|
*/ |
21
|
|
|
class RequestLimitRuleset |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** @var array */ |
25
|
|
|
private const STORAGE_MAP = [ |
26
|
|
|
'array' => ArrayAdapter::class, |
27
|
|
|
'laravel' => LaravelAdapter::class |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** @var array */ |
31
|
|
|
private const CACHE_STRATEGIES = [ |
32
|
|
|
'no-cache' => NoCache::class, |
33
|
|
|
'cache' => Cache::class, |
34
|
|
|
'force-cache' => ForceCache::class |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** @var array */ |
38
|
|
|
private $_rules; |
39
|
|
|
|
40
|
|
|
/** @var StorageInterface */ |
41
|
|
|
private $_storage; |
42
|
|
|
|
43
|
|
|
/** @var CacheStrategy */ |
44
|
|
|
private $_cacheStrategy; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* RequestLimitRuleset constructor. |
48
|
|
|
* @param array $rules |
49
|
|
|
* @param string $cacheStrategy |
50
|
|
|
* @param string|null $storageAdapter |
51
|
|
|
* @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownCacheStrategyException |
52
|
|
|
* @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownStorageAdapterException |
53
|
|
|
*/ |
54
|
10 |
|
public function __construct(array $rules, string $cacheStrategy = 'no-cache', string $storageAdapter = 'array') |
55
|
|
|
{ |
56
|
10 |
|
$this->_rules = $rules; |
57
|
10 |
|
$this->_setStorageAdapter($storageAdapter); |
58
|
9 |
|
$this->_setCacheStrategy($cacheStrategy); |
59
|
8 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $adapterName |
63
|
|
|
* @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownStorageAdapterException |
64
|
|
|
*/ |
65
|
10 |
|
private function _setStorageAdapter(string $adapterName) : void |
66
|
|
|
{ |
67
|
10 |
|
if (!isset(self::STORAGE_MAP[$adapterName])) |
68
|
|
|
{ |
69
|
1 |
|
throw new UnknownStorageAdapterException($adapterName, self::STORAGE_MAP); |
70
|
|
|
} |
71
|
|
|
|
72
|
9 |
|
$storageAdapterClass = self::STORAGE_MAP[$adapterName]; |
73
|
9 |
|
$this->_storage = new $storageAdapterClass(); |
74
|
9 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $cacheStrategy |
78
|
|
|
* @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownCacheStrategyException |
79
|
|
|
*/ |
80
|
9 |
|
private function _setCacheStrategy(string $cacheStrategy) : void |
81
|
|
|
{ |
82
|
9 |
|
if (!isset(self::CACHE_STRATEGIES[$cacheStrategy])) |
83
|
|
|
{ |
84
|
1 |
|
throw new UnknownCacheStrategyException($cacheStrategy, self::CACHE_STRATEGIES); |
85
|
|
|
} |
86
|
|
|
|
87
|
8 |
|
$cacheStrategyClass = self::CACHE_STRATEGIES[$cacheStrategy]; |
88
|
8 |
|
$this->_cacheStrategy = new $cacheStrategyClass($this->_storage); |
89
|
8 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array $rules |
93
|
|
|
* @param string $cacheStrategy |
94
|
|
|
* @param string $storageAdapter |
95
|
|
|
* @return static |
96
|
|
|
* @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownStorageAdapterException |
97
|
|
|
* @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownCacheStrategyException |
98
|
|
|
*/ |
99
|
4 |
|
public static function create(array $rules, string $cacheStrategy = 'no-cache', string $storageAdapter = 'array') |
100
|
|
|
{ |
101
|
4 |
|
return new static($rules, $cacheStrategy, $storageAdapter); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param RequestInterface $request |
106
|
|
|
* @param callable $handler |
107
|
|
|
* @return PromiseInterface |
108
|
|
|
*/ |
109
|
6 |
|
public function cache(RequestInterface $request, callable $handler) : PromiseInterface |
110
|
|
|
{ |
111
|
6 |
|
return $this->_cacheStrategy->request($request, $handler); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return RequestLimitGroup |
116
|
|
|
* @throws \Exception |
117
|
|
|
*/ |
118
|
7 |
|
public function getRequestLimitGroup() : RequestLimitGroup |
119
|
|
|
{ |
120
|
7 |
|
$requestLimitGroup = new RequestLimitGroup(); |
121
|
7 |
|
foreach ($this->_rules as $rule) |
122
|
|
|
{ |
123
|
7 |
|
$requestLimitGroup->addRequestLimiter(RequestLimiter::createFromRule($rule, $this->_storage)); |
124
|
|
|
} |
125
|
|
|
|
126
|
7 |
|
return $requestLimitGroup; |
127
|
|
|
} |
128
|
|
|
} |