|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters; |
|
4
|
|
|
|
|
5
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Interfaces\StorageInterface; |
|
6
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Helpers\RequestHelper; |
|
7
|
|
|
use Psr\Http\Message\RequestInterface; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class BaseAdapter |
|
12
|
|
|
* @package hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class BaseAdapter implements StorageInterface |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** @var int */ |
|
18
|
|
|
protected const DEFAULT_TTL = 300; |
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected const STORAGE_KEY = 'requests'; |
|
21
|
|
|
/** @var int Time To Live in minutes */ |
|
22
|
|
|
protected $_ttl = self::DEFAULT_TTL; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param RequestInterface $request |
|
26
|
|
|
* @param ResponseInterface $response |
|
27
|
|
|
* @throws \Exception |
|
28
|
|
|
*/ |
|
29
|
12 |
|
final public function saveResponse(RequestInterface $request, ResponseInterface $response) : void |
|
30
|
|
|
{ |
|
31
|
12 |
|
[$host, $path] = RequestHelper::getHostAndPath($request); |
|
32
|
|
|
|
|
33
|
12 |
|
$this->_saveResponse($response, $host, $path, RequestHelper::getStorageKey($request)); |
|
34
|
12 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param ResponseInterface $response |
|
38
|
|
|
* @param string $host |
|
39
|
|
|
* @param string $path |
|
40
|
|
|
* @param string $key |
|
41
|
|
|
*/ |
|
42
|
|
|
abstract protected function _saveResponse(ResponseInterface $response, string $host, string $path, string $key) : void; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param RequestInterface $request |
|
46
|
|
|
* @return ResponseInterface|null |
|
47
|
|
|
*/ |
|
48
|
17 |
|
final public function getResponse(RequestInterface $request) : ?ResponseInterface |
|
49
|
|
|
{ |
|
50
|
17 |
|
[$host, $path] = RequestHelper::getHostAndPath($request); |
|
51
|
|
|
|
|
52
|
17 |
|
return $this->_getResponse($host, $path, RequestHelper::getStorageKey($request)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $host |
|
57
|
|
|
* @param string $path |
|
58
|
|
|
* @param string $key |
|
59
|
|
|
* @return null|ResponseInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
abstract protected function _getResponse(string $host, string $path, string $key) : ?ResponseInterface; |
|
62
|
|
|
} |