Test Failed
Pull Request — master (#8)
by Timo
03:25
created

LaravelDriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
3
namespace hamburgscleanest\GuzzleAdvancedThrottle\Cache\Drivers;
4
5
use Illuminate\Container\Container;
6
7
8
/**
9
 * Class LaravelDriver
10
 * @package hamburgscleanest\GuzzleAdvancedThrottle\Cache\Drivers
11
 */
12
abstract class LaravelDriver
13
{
14
15
    /** @var string */
16
    private const DEFAULT_CACHE_PREFIX = 'throttle_cache';
17
    /** @var Container */
18
    protected $_container;
19 6
    /** @var string */
20
    protected $_driver;
21
    /** @var array */
22 6
    protected $_options;
23 6
    /** @var string */
24
    private $_driverStoreKey;
25
26
    /**
27
     * LaravelDriver constructor.
28
     * @param string $driver
29
     * @param array $options
30
     */
31
    public function __construct(string $driver, array $options = [])
32
    {
33
        $this->_container = new Container();
34
        $this->_driver = $driver;
35
        $this->_driverStoreKey = 'cache.stores.' . $this->_driver;
36
        $this->_options = $options;
37
    }
38
39
    /**
40
     * @return Container
41
     */
42
    public function getContainer() : Container
43
    {
44
        $this->_setConfig();
45
        $this->_setContainer();
46
47
        return $this->_container;
48
    }
49
50
    abstract protected function _setContainer() : void;
51
52
    private function _setConfig() : void
53
    {
54
        $this->_container['config'] = [
55
            'cache.default'        => $this->_driver,
56
            $this->_driverStoreKey => ['driver' => $this->_driver] + $this->_options,
57
            'cache.prefix'         => $this->_options['cache_prefix'] ?? self::DEFAULT_CACHE_PREFIX
58
        ];
59
    }
60
61
    /**
62
     * @param string $key
63
     * @param mixed $value
64
     */
65
    protected function _setStoreValue(string $key, $value) : void
66
    {
67
        $this->_container->offsetSet('config.' . $this->_driverStoreKey . '.' . $key, $value);
68
    }
69
}