Completed
Push — master ( 5db45e...963acd )
by Timo
02:41
created

BaseAdapter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 8
dl 0
loc 48
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponse() 0 5 1
A saveResponse() 0 5 1
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
}