CacheManager   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 61.11%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 42
ccs 11
cts 18
cp 0.6111
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A save() 0 4 1
A getItem() 0 13 3
A getItemPool() 0 8 2
1
<?php
2
3
namespace PhpAbac\Manager;
4
5
use Psr\Cache\{
6
    CacheItemInterface,
7
    CacheItemPoolInterface
8
};
9
10
class CacheManager implements CacheManagerInterface
11
{
12
    /** @var string **/
13
    protected $defaultDriver = 'memory';
14
    /** @var array **/
15
    protected $pools;
16
    /** @var array **/
17
    protected $options;
18
19 7
    public function __construct(array $options = [])
20
    {
21 7
        $this->options = $options;
22 7
    }
23
24 2
    public function save(CacheItemInterface $item)
25
    {
26 2
        $this->getItemPool($item->getDriver())->save($item);
27 2
    }
28
29
    public function getItem(string $key, string $driver = null, int $ttl = null): CacheItemInterface
30
    {
31
        $finalDriver = ($driver !== null) ? $driver : $this->defaultDriver;
32
33
        $pool = $this->getItemPool($finalDriver);
34
        $item = $pool->getItem($key);
35
36
        // In this case, the pool returned a new CacheItem
37
        if ($item->get() === null) {
38
            $item->expiresAfter($ttl);
39
        }
40
        return $item;
41
    }
42
43 3
    public function getItemPool(string $driver): CacheItemPoolInterface
44
    {
45 3
        if (!isset($this->pools[$driver])) {
46 3
            $poolClass = 'PhpAbac\\Cache\\Pool\\' . ucfirst($driver) . 'CacheItemPool';
47 3
            $this->pools[$driver] = new $poolClass($this->options);
48
        }
49 3
        return $this->pools[$driver];
50
    }
51
}
52