DoctrineCacheStorage::internalGetItem()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModule\Cache;
6
7
use Doctrine\Common\Cache\Cache;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
10
/**
11
 * Bridge class that allows usage of a Doctrine Cache Storage as a Laminas Cache Storage
12
 *
13
 * @link    http://www.doctrine-project.org/
14
 */
15
class DoctrineCacheStorage extends AbstractAdapter
16
{
17
    /** @var Cache */
18
    protected $cache;
19
20
    /**
21
     * {@inheritDoc}
22
     *
23
     * @param Cache $cache
24
     */
25 54
    public function __construct($options, Cache $cache)
26
    {
27 54
        parent::__construct($options);
28
29 54
        $this->cache = $cache;
30 54
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35 34
    protected function internalGetItem(&$normalizedKey, &$success = null, &$casToken = null)
36
    {
37 34
        $key     = $this->getOptions()->getNamespace() . $normalizedKey;
38 34
        $fetched = $this->cache->fetch($key);
39 34
        $success = ($fetched !== false);
40
41 34
        if ($success) {
42 27
            $casToken = $fetched;
43
44 27
            return $fetched;
45
        }
46
47 19
        return null;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53 35
    protected function internalSetItem(&$normalizedKey, &$value)
54
    {
55 35
        $key = $this->getOptions()->getNamespace() . $normalizedKey;
56 35
        $ttl = $this->getOptions()->getTtl();
57
58 35
        return $this->cache->save($key, $value, $ttl);
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64 6
    protected function internalRemoveItem(&$normalizedKey)
65
    {
66 6
        $key = $this->getOptions()->getNamespace() . $normalizedKey;
67 6
        if (! $this->cache->contains($key)) {
68 4
            return false;
69
        }
70
71 5
        return $this->cache->delete($key);
72
    }
73
}
74