Test Failed
Push — master ( 089bf1...f4e2c3 )
by Timo
02:20
created

LaravelAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 15
ccs 0
cts 5
cp 0
crap 2
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\Interfaces\StorageInterface;
7
use hamburgscleanest\GuzzleAdvancedThrottle\RequestInfo;
8
use Illuminate\Cache\CacheManager;
9
use Illuminate\Container\Container;
0 ignored issues
show
Bug introduced by
The type Illuminate\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Illuminate\Filesystem\Filesystem;
0 ignored issues
show
Bug introduced by
The type Illuminate\Filesystem\Filesystem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * Class LaravelAdapter
14
 * @package hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters
15
 */
16
class LaravelAdapter implements StorageInterface
17
{
18
19
    /** @var CacheManager */
20
    private $_cacheManager;
21
22
    public function __construct()
23
    {
24
        $container = new Container();
25
26
        // TODO: Set from outside..
27
        $container['config'] = [
28
            'cache.default'     => 'file',
29
            'cache.stores.file' => [
30
                'driver' => 'file',
31
                'path'   => __DIR__ . '/cache'
32
            ]
33
        ];
34
        $container['files'] = new Filesystem(); // TODO: Remove when config extracted..
35
36
        $this->_cacheManager = new CacheManager($container);
37
    }
38
39
    /**
40
     * @param string $host
41
     * @param string $key
42
     * @param int $requestCount
43
     * @param DateTime $expiresAt
44
     * @param int $remainingSeconds
45
     */
46
    public function save(string $host, string $key, int $requestCount, DateTime $expiresAt, int $remainingSeconds) : void
47
    {
48
        $this->_cacheManager->put(
49
            $this->_buildKey($host, $key),
50
            RequestInfo::create($requestCount, $expiresAt->getTimestamp(), $remainingSeconds),
51
            $remainingSeconds
52
        );
53
    }
54
55
    /**
56
     * @param string $host
57
     * @param string $key
58
     * @return string
59
     */
60
    private function _buildKey(string $host, string $key) : string
61
    {
62
        return $host . '.' . $key;
63
    }
64
65
    /**
66
     * @param string $host
67
     * @param string $key
68
     * @return RequestInfo|null
69
     */
70
    public function get(string $host, string $key) : ? RequestInfo
71
    {
72
        /** @noinspection PhpIncompatibleReturnTypeInspection */
73
        return $this->_cacheManager->get($this->_buildKey($host, $key));
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->_cacheMana..._buildKey($host, $key)) returns the type Illuminate\Contracts\Cache\Repository which is incompatible with the type-hinted return null|hamburgscleanest\Gu...cedThrottle\RequestInfo.
Loading history...
74
    }
75
}