ArrayAdapter::_getResponse()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 10
cc 4
nc 4
nop 3
crap 4
1
<?php
2
3
namespace hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters;
4
5
use GuzzleHttp\Psr7\Response;
6
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\CachedResponse;
7
use hamburgscleanest\GuzzleAdvancedThrottle\RequestInfo;
8
use hamburgscleanest\GuzzleAdvancedThrottle\SystemClock;
9
use hamburgscleanest\GuzzleAdvancedThrottle\TimeKeeper;
10
use Psr\Http\Message\ResponseInterface;
11
use Illuminate\Config\Repository;
12
13
class ArrayAdapter extends BaseAdapter
14
{
15
    /** @var string */
16
    private const RESPONSE_KEY = 'response';
17
    /** @var string */
18
    private const EXPIRATION_KEY = 'expires_at';
19
20
    private array $_storage = [];
21
22 38
    public function __construct(?Repository $config = null)
23
    {
24 38
        if ($config === null) {
25 35
            return;
26
        }
27
28 3
        $this->_ttl = $config->get('cache.ttl', self::DEFAULT_TTL);
29 3
        $this->_allowEmptyValues = $config->get('cache.allow_empty', $this->_allowEmptyValues);
30 3
    }
31
32 20
    public function save(string $host, string $key, int $requestCount, TimeKeeper $timeKeeper): void
33
    {
34 20
        $expiration = $timeKeeper->getExpiration();
35 20
        if ($expiration === null) {
36 2
            unset($this->_storage[$host][$key]);
37 2
            return;
38
        }
39
40 19
        $this->_storage[$host][$key] = RequestInfo::create(
41 19
            $requestCount,
42 19
            $expiration->getTimestamp(),
43 19
            $timeKeeper->getRemainingSeconds()
44
        );
45 19
    }
46
47 28
    public function get(string $host, string $key): ?RequestInfo
48
    {
49 28
        return $this->_storage[$host][$key] ?? null;
50
    }
51
52 9
    protected function _saveResponse(ResponseInterface $response, string $host, string $path, string $key): void
53
    {
54 9
        $this->_storage[self::STORAGE_KEY][$host][$path][$key] = [
55 9
            self::RESPONSE_KEY   => new CachedResponse($response),
56 9
            self::EXPIRATION_KEY => SystemClock::create()->advanceMinutes($this->_ttl)->now()->getTimestamp()
57
        ];
58 9
    }
59
60 12
    protected function _getResponse(string $host, string $path, string $key): ?Response
61
    {
62 12
        $response = $this->_storage[self::STORAGE_KEY][$host][$path][$key] ?? null;
63
64 12
        if ($response !== null) {
65 8
            if ($response[self::EXPIRATION_KEY] > \time()) {
66
                /** @var CachedResponse|null $cachedResponse */
67 7
                $cachedResponse = $response[self::RESPONSE_KEY];
68
69 7
                return $cachedResponse ? $cachedResponse->getResponse() : null;
70
            }
71
72 1
            unset($this->_storage[self::STORAGE_KEY][$host][$path][$key]);
73
        }
74
75 8
        return null;
76
    }
77
}
78