Passed
Push — master ( 5eeac1...edca40 )
by Timo
04:35
created

LaravelAdapter::_getCacheManager()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 3
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 5
rs 8.8571
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\RequestHelper;
7
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Interfaces\StorageInterface;
8
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheConfigNotSetException;
9
use hamburgscleanest\GuzzleAdvancedThrottle\RequestInfo;
10
use Illuminate\Cache\CacheManager;
11
use Illuminate\Config\Repository;
12
use Illuminate\Container\Container;
13
use Illuminate\Filesystem\Filesystem;
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * Class LaravelAdapter
19
 * @package hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters
20
 */
21
class LaravelAdapter implements StorageInterface
22
{
23
24
    /** @var string */
25
    private const STORAGE_KEY = 'requests';
26
    /** @var CacheManager */
27
    private $_cacheManager;
28
29
    /**
30
     * LaravelAdapter constructor.
31
     * @param Repository|null $config
32
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheConfigNotSetException
33
     */
34 3
    public function __construct(?Repository $config = null)
35
    {
36 3
        $this->_cacheManager = $this->_getCacheManager($config);
37 2
    }
38
39
    /**
40
     * @param Repository|null $config
41
     * @return CacheManager
42
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheConfigNotSetException
43
     */
44 3
    private function _getCacheManager(Repository $config = null) : CacheManager
45
    {
46
        /** @var Repository $storageConfig */
47 3
        $storageConfig = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $storageConfig is dead and can be removed.
Loading history...
48 3
        if ($config === null || ($storageConfig = new Repository($config->get('storage'))) === null)
49
        {
50 1
            throw new LaravelCacheConfigNotSetException();
51
        }
52
53 2
        $container = new Container();
54 2
        $store = $storageConfig->get('cache.stores.file');
55 2
        if ($store !== null && $store['driver'] === 'file')
56
        {
57 2
            $container['files'] = new Filesystem();
58
        }
59
60 2
        $container['config'] = $storageConfig->all();
61
62 2
        return new CacheManager($container);
63
    }
64
65
    /**
66
     * @param string $host
67
     * @param string $key
68
     * @param int $requestCount
69
     * @param DateTime $expiresAt
70
     * @param int $remainingSeconds
71
     */
72 1
    public function save(string $host, string $key, int $requestCount, DateTime $expiresAt, int $remainingSeconds) : void
73
    {
74 1
        $this->_cacheManager->put(
75 1
            $this->_buildKey($host, $key),
76 1
            RequestInfo::create($requestCount, $expiresAt->getTimestamp(), $remainingSeconds),
77 1
            $remainingSeconds / 60
78
        );
79 1
    }
80
81
    /**
82
     * @param string $host
83
     * @param string $key
84
     * @return string
85
     */
86 2
    private function _buildKey(string $host, string $key) : string
87
    {
88 2
        return $host . '.' . $key;
89
    }
90
91
    /**
92
     * @param string $host
93
     * @param string $key
94
     * @return RequestInfo|null
95
     */
96 1
    public function get(string $host, string $key) : ? RequestInfo
97
    {
98
        /** @noinspection PhpIncompatibleReturnTypeInspection */
99 1
        return $this->_cacheManager->get($this->_buildKey($host, $key));
100
    }
101
102
    /**
103
     * @param RequestInterface $request
104
     * @param ResponseInterface $response
105
     * @param int $duration
106
     * @throws \Exception
107
     */
108 1
    public function saveResponse(RequestInterface $request, ResponseInterface $response, int $duration = 300) : void
109
    {
110 1
        [$host, $path] = RequestHelper::getHostAndPath($request);
111
112 1
        $this->_cacheManager->put($this->_buildResponseKey($host, $path), $response, $duration);
113 1
    }
114
115
    /**
116
     * @param string $host
117
     * @param string $path
118
     * @return string
119
     */
120 1
    private function _buildResponseKey(string $host, string $path) : string
121
    {
122 1
        return self::STORAGE_KEY . '.' . $this->_buildKey($host, $path);
123
    }
124
125
    /**
126
     * @param RequestInterface $request
127
     * @return ResponseInterface|null
128
     */
129 1
    public function getResponse(RequestInterface $request) : ? ResponseInterface
130
    {
131 1
        [$host, $path] = RequestHelper::getHostAndPath($request);
132
133
        /** @noinspection PhpIncompatibleReturnTypeInspection */
134 1
        return $this->_cacheManager->get($this->_buildResponseKey($host, $path));
135
    }
136
}