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\HostNotDefinedException; |
14
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownCacheStrategyException; |
15
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownStorageAdapterException; |
16
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Helpers\InterfaceHelper; |
17
|
|
|
use Illuminate\Config\Repository; |
18
|
|
|
use Psr\Http\Message\RequestInterface; |
19
|
|
|
|
20
|
|
|
class RequestLimitRuleset |
21
|
|
|
{ |
22
|
|
|
/** @var array */ |
23
|
|
|
private const STORAGE_MAP = [ |
24
|
|
|
'array' => ArrayAdapter::class, |
25
|
|
|
'laravel' => LaravelAdapter::class |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
private const CACHE_STRATEGIES = [ |
30
|
|
|
'no-cache' => NoCache::class, |
31
|
|
|
'cache' => Cache::class, |
32
|
|
|
'force-cache' => ForceCache::class |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
private StorageInterface $_storage; |
36
|
|
|
private CacheStrategy $_cacheStrategy; |
37
|
|
|
private RequestLimitGroup $_requestLimitGroup; |
38
|
|
|
|
39
|
33 |
|
public function __construct(array $rules, string $cacheStrategy = 'no-cache', string $storageAdapter = 'array', Repository $config = null) |
40
|
|
|
{ |
41
|
33 |
|
$this->_setStorageAdapter($storageAdapter, $config); |
42
|
27 |
|
$this->_setCacheStrategy($cacheStrategy); |
43
|
25 |
|
$this->_setRequestLimitGroup($rules); |
44
|
24 |
|
} |
45
|
|
|
|
46
|
33 |
|
private function _setStorageAdapter(string $adapterName, ?Repository $config): void |
47
|
|
|
{ |
48
|
33 |
|
$adapterClassName = self::STORAGE_MAP[$adapterName] ?? $adapterName; |
49
|
|
|
|
50
|
33 |
|
if (!InterfaceHelper::implementsInterface($adapterClassName, StorageInterface::class)) { |
51
|
2 |
|
throw new UnknownStorageAdapterException($adapterClassName, \array_values(self::STORAGE_MAP)); |
52
|
|
|
} |
53
|
|
|
|
54
|
31 |
|
$this->_storage = new $adapterClassName($config); |
55
|
27 |
|
} |
56
|
|
|
|
57
|
27 |
|
private function _setCacheStrategy(string $cacheStrategy): void |
58
|
|
|
{ |
59
|
27 |
|
$cacheStrategyClassName = self::CACHE_STRATEGIES[$cacheStrategy] ?? $cacheStrategy; |
60
|
|
|
|
61
|
27 |
|
if (!InterfaceHelper::implementsInterface($cacheStrategyClassName, CacheStrategy::class)) { |
62
|
2 |
|
throw new UnknownCacheStrategyException($cacheStrategyClassName, \array_values(self::CACHE_STRATEGIES)); |
63
|
|
|
} |
64
|
|
|
|
65
|
25 |
|
$this->_cacheStrategy = new $cacheStrategyClassName($this->_storage); |
66
|
25 |
|
} |
67
|
|
|
|
68
|
10 |
|
public static function create(array $rules, string $cacheStrategy = 'no-cache', string $storageAdapter = 'array') |
69
|
|
|
{ |
70
|
10 |
|
return new static($rules, $cacheStrategy, $storageAdapter); |
71
|
|
|
} |
72
|
|
|
|
73
|
19 |
|
public function cache(RequestInterface $request, callable $handler): PromiseInterface |
74
|
|
|
{ |
75
|
19 |
|
return $this->_cacheStrategy->request($request, $handler); |
76
|
|
|
} |
77
|
|
|
|
78
|
25 |
|
private function _setRequestLimitGroup(array $ruleGroup): void |
79
|
|
|
{ |
80
|
25 |
|
$this->_requestLimitGroup = new RequestLimitGroup(); |
81
|
25 |
|
foreach ($ruleGroup as $host => $rules) { |
82
|
21 |
|
if (!\is_string($host)) { |
83
|
1 |
|
throw new HostNotDefinedException(); |
84
|
|
|
} |
85
|
|
|
|
86
|
20 |
|
$this->_requestLimitGroup->addRules($host, $rules, $this->_storage); |
87
|
|
|
} |
88
|
24 |
|
} |
89
|
|
|
|
90
|
20 |
|
public function getRequestLimitGroup(): RequestLimitGroup |
91
|
|
|
{ |
92
|
20 |
|
return $this->_requestLimitGroup; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|