Completed
Push — master ( 3d8aa7...80558b )
by Timo
02:25
created

LaravelAdapter::saveResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
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\RequestInfo;
9
use Illuminate\Cache\CacheManager;
10
use Illuminate\Container\Container;
11
use Illuminate\Filesystem\Filesystem;
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 2
    public function __construct()
28
    {
29 2
        $container = new Container();
30
31
        // TODO: Set from outside..
32 2
        $container['config'] = [
33
            'cache.default'     => 'file',
34
            'cache.stores.file' => [
35
                'driver' => 'file',
36
                'path'   => __DIR__ . '/cache'
37
            ]
38
        ];
39 2
        $container['files'] = new Filesystem(); // TODO: Remove when config extracted..
40
41 2
        $this->_cacheManager = new CacheManager($container);
42 2
    }
43
44
    /**
45
     * @param string $host
46
     * @param string $key
47
     * @param int $requestCount
48
     * @param DateTime $expiresAt
49
     * @param int $remainingSeconds
50
     */
51 1
    public function save(string $host, string $key, int $requestCount, DateTime $expiresAt, int $remainingSeconds) : void
52
    {
53 1
        $this->_cacheManager->put(
54 1
            $this->_buildKey($host, $key),
55 1
            RequestInfo::create($requestCount, $expiresAt->getTimestamp(), $remainingSeconds),
56 1
            $remainingSeconds
57
        );
58 1
    }
59
60
    /**
61
     * @param string $host
62
     * @param string $key
63
     * @return string
64
     */
65 2
    private function _buildKey(string $host, string $key) : string
66
    {
67 2
        return $host . '.' . $key;
68
    }
69
70
    /**
71
     * @param string $host
72
     * @param string $key
73
     * @return RequestInfo|null
74
     */
75 1
    public function get(string $host, string $key) : ? RequestInfo
76
    {
77
        /** @noinspection PhpIncompatibleReturnTypeInspection */
78 1
        return $this->_cacheManager->get($this->_buildKey($host, $key));
79
    }
80
81
    /**
82
     * @param RequestInterface $request
83
     * @param ResponseInterface $response
84
     * @param int $duration
85
     * @throws \Exception
86
     */
87 1
    public function saveResponse(RequestInterface $request, ResponseInterface $response, int $duration = 300) : void
88
    {
89 1
        [$host, $path] = RequestHelper::getHostAndPath($request);
90
91 1
        $this->_cacheManager->put($this->_buildResponseKey($host, $path), $response, $duration);
92 1
    }
93
94
    /**
95
     * @param string $host
96
     * @param string $path
97
     * @return string
98
     */
99 1
    private function _buildResponseKey(string $host, string $path) : string
100
    {
101 1
        return self::STORAGE_KEY . '.' . $this->_buildKey($host, $path);
102
    }
103
104
    /**
105
     * @param RequestInterface $request
106
     * @return ResponseInterface|null
107
     */
108 1
    public function getResponse(RequestInterface $request) : ? ResponseInterface
109
    {
110 1
        [$host, $path] = RequestHelper::getHostAndPath($request);
111
112
        /** @noinspection PhpIncompatibleReturnTypeInspection */
113 1
        return $this->_cacheManager->get($this->_buildResponseKey($host, $path));
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->_cacheMana...ponseKey($host, $path)) returns the type Illuminate\Contracts\Cache\Repository which is incompatible with the type-hinted return null|Psr\Http\Message\ResponseInterface.
Loading history...
114
    }
115
}