CacheManager::getItem()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 9.8333
c 0
b 0
f 0
cc 3
nc 4
nop 3
crap 12
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