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

LaravelAdapter::saveResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters;
4
5
use DateTime;
6
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Helpers\CacheConfigHelper;
7
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheConfigNotSetException;
8
use hamburgscleanest\GuzzleAdvancedThrottle\RequestInfo;
9
use Illuminate\Cache\CacheManager;
10
use Illuminate\Config\Repository;
11
use Psr\Http\Message\ResponseInterface;
12
13
/**
14
 * Class LaravelAdapter
15
 * @package hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters
16
 */
17
class LaravelAdapter extends BaseAdapter
18
{
19
20
    /** @var CacheManager */
21
    private $_cacheManager;
22
23
    /**
24
     * LaravelAdapter constructor.
25
     * @param Repository|null $config
26
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheDriverNotSetException
27
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheConfigNotSetException
28
     */
29 14
    public function __construct(?Repository $config = null)
30
    {
31 14
        if ($config === null || ($cacheConfig = $config->get('cache')) === null)
32
        {
33 2
            throw new LaravelCacheConfigNotSetException();
34
        }
35
36 12
        $cacheRepository = new Repository($cacheConfig);
37 12
        $this->_cacheManager = CacheConfigHelper::getCacheManager($cacheRepository);
38 9
        $this->_ttl = $cacheRepository->get('ttl', self::DEFAULT_TTL);
39 9
    }
40
41
    /**
42
     * @param string $host
43
     * @param string $key
44
     * @param int $requestCount
45
     * @param DateTime $expiresAt
46
     * @param int $remainingSeconds
47
     */
48 4
    public function save(string $host, string $key, int $requestCount, DateTime $expiresAt, int $remainingSeconds) : void
49
    {
50 4
        $this->_cacheManager->put(
51 4
            $this->_buildKey($host, $key),
52 4
            RequestInfo::create($requestCount, $expiresAt->getTimestamp(), $remainingSeconds),
53 4
            $remainingSeconds / 60
54
        );
55 4
    }
56
57
    /**
58
     * @param string $host
59
     * @param string $key
60
     * @return string
61
     */
62 7
    private function _buildKey(string $host, string $key) : string
63
    {
64 7
        return $host . '.' . $key;
65
    }
66
67
    /**
68
     * @param string $host
69
     * @param string $key
70
     * @return RequestInfo|null
71
     */
72 7
    public function get(string $host, string $key) : ?RequestInfo
73
    {
74
        /** @noinspection PhpIncompatibleReturnTypeInspection */
75 7
        return $this->_cacheManager->get($this->_buildKey($host, $key));
76
    }
77
78
    /**
79
     * @param ResponseInterface $response
80
     * @param string $host
81
     * @param string $path
82
     * @param string $key
83
     */
84 5
    protected function _saveResponse(ResponseInterface $response, string $host, string $path, string $key) : void
85
    {
86 5
        $this->_cacheManager->put($this->_buildResponseKey($host, $path, $key), $response, $this->_ttl);
87 5
    }
88
89
    /**
90
     * @param string $host
91
     * @param string $path
92
     * @param string $key
93
     * @return string
94
     */
95 8
    private function _buildResponseKey(string $host, string $path, string $key) : string
96
    {
97 8
        return self::STORAGE_KEY . '.' . $host . '.' . $path . '.' . $key;
98
    }
99
100
    /**
101
     * @param string $host
102
     * @param string $path
103
     * @param string $key
104
     * @return null|ResponseInterface
105
     */
106 8
    protected function _getResponse(string $host, string $path, string $key) : ?ResponseInterface
107
    {
108
        /** @noinspection PhpIncompatibleReturnTypeInspection */
109 8
        return $this->_cacheManager->get($this->_buildResponseKey($host, $path, $key));
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->_cacheMana...ey($host, $path, $key)) returns the type Illuminate\Contracts\Cache\Repository which is incompatible with the type-hinted return null|Psr\Http\Message\ResponseInterface.
Loading history...
110
    }
111
}