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

ArrayAdapter::getResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A ArrayAdapter::_invalidate() 0 3 1
1
<?php
2
3
namespace hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters;
4
5
use DateInterval;
6
use DateTime;
7
use hamburgscleanest\GuzzleAdvancedThrottle\RequestInfo;
8
use Psr\Http\Message\ResponseInterface;
9
use Illuminate\Config\Repository;
10
11
/**
12
 * Class ArrayAdapter
13
 * @package hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters
14
 */
15
class ArrayAdapter extends BaseAdapter
16
{
17
    /** @var string */
18
    private const RESPONSE_KEY = 'response';
19
    /** @var string */
20
    private const EXPIRATION_KEY = 'expires_at';
21
    /** @var array */
22
    private $_storage = [];
23
24
    /**
25
     * StorageInterface constructor.
26
     * @param Repository|null $config
27
     */
28 33
    public function __construct(?Repository $config = null)
29
    {
30 33
        if ($config === null)
31
        {
32 32
            return;
33
        }
34
35 1
        $this->_ttl = $config->get('cache.ttl', self::DEFAULT_TTL);
36 1
    }
37
38
    /**
39
     * @param string $host
40
     * @param string $key
41
     * @param int $requestCount
42
     * @param \DateTime $expiresAt
43
     * @param int $remainingSeconds
44
     */
45 17
    public function save(string $host, string $key, int $requestCount, DateTime $expiresAt, int $remainingSeconds) : void
46
    {
47 17
        $this->_storage[$host][$key] = RequestInfo::create($requestCount, $expiresAt->getTimestamp(), $remainingSeconds);
48 17
    }
49
50
    /**
51
     * @param string $host
52
     * @param string $key
53
     * @return RequestInfo|null
54
     */
55 25
    public function get(string $host, string $key) : ?RequestInfo
56
    {
57 25
        return $this->_storage[$host][$key] ?? null;
58
    }
59
60
    /**
61
     * @param ResponseInterface $response
62
     * @param string $host
63
     * @param string $path
64
     * @param string $key
65
     * @param int $expiresAt
66
     * @throws \Exception
67
     */
68 7
    protected function _saveResponse(ResponseInterface $response, string $host, string $path, string $key) : void
69
    {
70 7
        $this->_storage[self::STORAGE_KEY][$host][$path][$key] = [
71 7
            self::RESPONSE_KEY   => $response,
72 7
            self::EXPIRATION_KEY => (new DateTime())->add(new DateInterval('PT' . $this->_ttl . 'M'))->getTimestamp()
73
        ];
74 7
    }
75
76
    /**
77
     * @param string $host
78
     * @param string $path
79
     * @param string $key
80
     * @return null|ResponseInterface
81
     */
82 9
    protected function _getResponse(string $host, string $path, string $key) : ?ResponseInterface
83
    {
84 9
        $response = $this->_storage[self::STORAGE_KEY][$host][$path][$key] ?? null;
85 9
        if ($response !== null)
86
        {
87 7
            if ($response[self::EXPIRATION_KEY] > \time())
88
            {
89 6
                return $response[self::RESPONSE_KEY];
90
            }
91
92 1
            $this->_invalidate($host, $path, $key);
93
        }
94
95 6
        return null;
96
    }
97
98
    /**
99
     * @param string $host
100
     * @param string $path
101
     * @param string $key
102
     */
103 1
    private function _invalidate(string $host, string $path, string $key) : void
104
    {
105 1
        unset($this->_storage[self::STORAGE_KEY][$host][$path][$key]);
106
    }
107
}