Completed
Push — master ( befab8...0495c3 )
by Timo
02:25
created

LaravelAdapter::_getContainer()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
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 11
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A LaravelAdapter::get() 0 4 1
1
<?php
2
3
namespace hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters;
4
5
use DateTime;
6
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Helpers\CacheConfigHelper;
7
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Helpers\RequestHelper;
8
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Interfaces\StorageInterface;
9
use hamburgscleanest\GuzzleAdvancedThrottle\RequestInfo;
10
use Illuminate\Cache\CacheManager;
11
use Illuminate\Config\Repository;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
15
/**
16
 * Class LaravelAdapter
17
 * @package hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters
18
 */
19
class LaravelAdapter implements StorageInterface
20
{
21
22
    /** @var string */
23
    private const STORAGE_KEY = 'requests';
24
    /** @var CacheManager */
25
    private $_cacheManager;
26
27
    /**
28
     * LaravelAdapter constructor.
29
     * @param Repository|null $config
30
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheDriverNotSetException
31
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheConfigNotSetException
32
     */
33 3
    public function __construct(?Repository $config = null)
34
    {
35 3
        $this->_cacheManager = CacheConfigHelper::getCacheManager($config);
36 2
    }
37
38
    /**
39
     * @param string $host
40
     * @param string $key
41
     * @param int $requestCount
42
     * @param DateTime $expiresAt
43
     * @param int $remainingSeconds
44
     */
45 1
    public function save(string $host, string $key, int $requestCount, DateTime $expiresAt, int $remainingSeconds) : void
46
    {
47 1
        $this->_cacheManager->put(
48 1
            $this->_buildKey($host, $key),
49 1
            RequestInfo::create($requestCount, $expiresAt->getTimestamp(), $remainingSeconds),
50 1
            $remainingSeconds / 60
51
        );
52 1
    }
53
54
    /**
55
     * @param string $host
56
     * @param string $key
57
     * @return string
58
     */
59 2
    private function _buildKey(string $host, string $key) : string
60
    {
61 2
        return $host . '.' . $key;
62
    }
63
64
    /**
65
     * @param string $host
66
     * @param string $key
67
     * @return RequestInfo|null
68
     */
69 1
    public function get(string $host, string $key) : ? RequestInfo
70
    {
71
        /** @noinspection PhpIncompatibleReturnTypeInspection */
72 1
        return $this->_cacheManager->get($this->_buildKey($host, $key));
73
    }
74
75
    /**
76
     * @param RequestInterface $request
77
     * @param ResponseInterface $response
78
     * @param int $duration
79
     * @throws \Exception
80
     */
81 1
    public function saveResponse(RequestInterface $request, ResponseInterface $response, int $duration = 300) : void
82
    {
83 1
        [$host, $path] = RequestHelper::getHostAndPath($request);
84
85 1
        $this->_cacheManager->put($this->_buildResponseKey($host, $path), $response, $duration);
86 1
    }
87
88
    /**
89
     * @param string $host
90
     * @param string $path
91
     * @return string
92
     */
93 1
    private function _buildResponseKey(string $host, string $path) : string
94
    {
95 1
        return self::STORAGE_KEY . '.' . $this->_buildKey($host, $path);
96
    }
97
98
    /**
99
     * @param RequestInterface $request
100
     * @return ResponseInterface|null
101
     */
102 1
    public function getResponse(RequestInterface $request) : ? ResponseInterface
103
    {
104 1
        [$host, $path] = RequestHelper::getHostAndPath($request);
105
106
        /** @noinspection PhpIncompatibleReturnTypeInspection */
107 1
        return $this->_cacheManager->get($this->_buildResponseKey($host, $path));
108
    }
109
}