LaravelAdapter::_getResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
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\Cache\Helpers\CacheConfigHelper;
8
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheConfigNotSetException;
9
use hamburgscleanest\GuzzleAdvancedThrottle\RequestInfo;
10
use hamburgscleanest\GuzzleAdvancedThrottle\TimeKeeper;
11
use Illuminate\Cache\CacheManager;
12
use Illuminate\Config\Repository;
13
use Psr\Http\Message\ResponseInterface;
14
15
class LaravelAdapter extends BaseAdapter
16
{
17
    private CacheManager $_cacheManager;
18
19 15
    public function __construct(?Repository $config = null)
20
    {
21 15
        if ($config === null || ($cacheConfig = $config->get('cache')) === null) {
22 2
            throw new LaravelCacheConfigNotSetException();
23
        }
24
25 13
        $cacheRepository = new Repository($cacheConfig);
26 13
        $this->_cacheManager = CacheConfigHelper::getCacheManager($cacheRepository);
27 10
        $this->_ttl = $cacheRepository->get('ttl', self::DEFAULT_TTL);
28 10
        $this->_allowEmptyValues = $cacheRepository->get('allow_empty', $this->_allowEmptyValues);
29 10
    }
30
31 4
    public function save(string $host, string $key, int $requestCount, TimeKeeper $timeKeeper): void
32
    {
33 4
        $expiration = $timeKeeper->getExpiration();
34 4
        if ($expiration === null) {
35 1
            $this->_cacheManager->forget($this->_buildKey($host, $key));
36
37 1
            return;
38
        }
39
40 4
        $remainingSeconds = $timeKeeper->getRemainingSeconds();
41
42 4
        $this->_cacheManager->put(
43 4
            $this->_buildKey($host, $key),
44 4
            RequestInfo::create(
45 4
                $requestCount,
46 4
                $expiration->getTimestamp(),
47 4
                $remainingSeconds
48
            ),
49
            $remainingSeconds
50
        );
51 4
    }
52
53 7
    private function _buildKey(string $host, string $key): string
54
    {
55 7
        return $host . '.' . $key;
56
    }
57
58 7
    public function get(string $host, string $key): ?RequestInfo
59
    {
60 7
        return $this->_cacheManager->get($this->_buildKey($host, $key));
61
    }
62
63 5
    protected function _saveResponse(ResponseInterface $response, string $host, string $path, string $key): void
64
    {
65 5
        $this->_cacheManager->put(
66 5
            $this->_buildResponseKey($host, $path, $key),
67 5
            new CachedResponse($response),
68 5
            $this->_ttl
69
        );
70 5
    }
71
72 9
    private function _buildResponseKey(string $host, string $path, string $key): string
73
    {
74 9
        return self::STORAGE_KEY . '.' . $host . '.' . $path . '.' . $key;
75
    }
76
77 9
    protected function _getResponse(string $host, string $path, string $key): ?Response
78
    {
79
        /** @var CachedResponse|null $cachedResponse */
80 9
        $cachedResponse = $this->_cacheManager->get($this->_buildResponseKey($host, $path, $key));
81
82 9
        return $cachedResponse ? $cachedResponse->getResponse() : null;
83
    }
84
}
85