Completed
Push — master ( fa94a3...0a0472 )
by Timo
02:23
created

LaravelAdapter::_getCacheManager()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
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 3
        if ($config === null || ($storageConfig = new Repository($config->get('storage'))) === null)
47
        {
48 1
            throw new LaravelCacheConfigNotSetException();
49
        }
50
51 2
        $container = $this->_getContainer($storageConfig);
52
53 2
        $container['config'] = $storageConfig->all();
54
55 2
        return new CacheManager($container);
56
    }
57
58
    /**
59
     * @param Repository $config
60
     * @return Container
61
     */
62 2
    private function _getContainer(Repository $config) : Container
63
    {
64 2
        $container = new Container();
65
66 2
        $store = $config->get('cache.stores.file');
67 2
        if ($store !== null && $store['driver'] === 'file')
68
        {
69 2
            $container['files'] = new Filesystem();
70
        }
71
72 2
        return $container;
73
    }
74
75
    /**
76
     * @param string $host
77
     * @param string $key
78
     * @param int $requestCount
79
     * @param DateTime $expiresAt
80
     * @param int $remainingSeconds
81
     */
82 1
    public function save(string $host, string $key, int $requestCount, DateTime $expiresAt, int $remainingSeconds) : void
83
    {
84 1
        $this->_cacheManager->put(
85 1
            $this->_buildKey($host, $key),
86 1
            RequestInfo::create($requestCount, $expiresAt->getTimestamp(), $remainingSeconds),
87 1
            $remainingSeconds / 60
88
        );
89 1
    }
90
91
    /**
92
     * @param string $host
93
     * @param string $key
94
     * @return string
95
     */
96 2
    private function _buildKey(string $host, string $key) : string
97
    {
98 2
        return $host . '.' . $key;
99
    }
100
101
    /**
102
     * @param string $host
103
     * @param string $key
104
     * @return RequestInfo|null
105
     */
106 1
    public function get(string $host, string $key) : ? RequestInfo
107
    {
108
        /** @noinspection PhpIncompatibleReturnTypeInspection */
109 1
        return $this->_cacheManager->get($this->_buildKey($host, $key));
110
    }
111
112
    /**
113
     * @param RequestInterface $request
114
     * @param ResponseInterface $response
115
     * @param int $duration
116
     * @throws \Exception
117
     */
118 1
    public function saveResponse(RequestInterface $request, ResponseInterface $response, int $duration = 300) : void
119
    {
120 1
        [$host, $path] = RequestHelper::getHostAndPath($request);
121
122 1
        $this->_cacheManager->put($this->_buildResponseKey($host, $path), $response, $duration);
123 1
    }
124
125
    /**
126
     * @param string $host
127
     * @param string $path
128
     * @return string
129
     */
130 1
    private function _buildResponseKey(string $host, string $path) : string
131
    {
132 1
        return self::STORAGE_KEY . '.' . $this->_buildKey($host, $path);
133
    }
134
135
    /**
136
     * @param RequestInterface $request
137
     * @return ResponseInterface|null
138
     */
139 1
    public function getResponse(RequestInterface $request) : ? ResponseInterface
140
    {
141 1
        [$host, $path] = RequestHelper::getHostAndPath($request);
142
143
        /** @noinspection PhpIncompatibleReturnTypeInspection */
144 1
        return $this->_cacheManager->get($this->_buildResponseKey($host, $path));
145
    }
146
}