DoctrineCacheStorage   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 59
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A internalGetItem() 0 14 2
A internalSetItem() 0 7 1
A internalRemoveItem() 0 9 2
A __construct() 0 6 1
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