Completed
Push — develop ( f3c8ea...81dc89 )
by Tom
16s queued 10s
created

LaminasStorageCache   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.15%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 79
ccs 25
cts 26
cp 0.9615
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A doFetch() 0 6 1
A doContains() 0 4 1
A doSave() 0 5 1
A doDelete() 0 4 1
A doFlush() 0 10 2
A doGetStats() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModule\Cache;
6
7
use Doctrine\Common\Cache\Cache;
8
use Doctrine\Common\Cache\CacheProvider;
9
use Laminas\Cache\Storage\AvailableSpaceCapableInterface;
10
use Laminas\Cache\Storage\FlushableInterface;
11
use Laminas\Cache\Storage\StorageInterface;
12
use Laminas\Cache\Storage\TotalSpaceCapableInterface;
13
14
/**
15
 * Bridge class that allows usage of a Laminas Cache Storage as a Doctrine Cache
16
 *
17
 * @link    http://www.doctrine-project.org/
18
 */
19
class LaminasStorageCache extends CacheProvider
20
{
21
    /** @var StorageInterface */
22
    protected $storage;
23
24 5
    public function __construct(StorageInterface $storage)
25
    {
26 5
        $this->storage = $storage;
27 5
    }
28
29
    /**
30
     * {@inheritDoc}
31
     */
32 4
    protected function doFetch($id)
33
    {
34 4
        $hit = $this->storage->getItem($id);
35
36 4
        return $hit ?? false;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42 4
    protected function doContains($id)
43
    {
44 4
        return $this->storage->hasItem($id);
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 4
    protected function doSave($id, $data, $lifeTime = false)
51
    {
52
        // @todo check if lifetime can be set
53 4
        return $this->storage->setItem($id, $data);
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59 1
    protected function doDelete($id)
60
    {
61 1
        return $this->storage->removeItem($id);
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 1
    protected function doFlush()
68
    {
69 1
        if ($this->storage instanceof FlushableInterface) {
70 1
            $storage = $this->storage;
71
72 1
            return $storage->flush();
73
        }
74
75
        return false;
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81 1
    protected function doGetStats()
82
    {
83 1
        $storage = $this->storage;
84
85
        return [
86 1
            Cache::STATS_HITS              => $this->storage->getMetadata(Cache::STATS_HITS),
87 1
            Cache::STATS_MISSES            => $this->storage->getMetadata(Cache::STATS_MISSES),
88 1
            Cache::STATS_UPTIME            => $this->storage->getMetadata(Cache::STATS_UPTIME),
89 1
            Cache::STATS_MEMORY_USAGE      => $storage instanceof TotalSpaceCapableInterface
90 1
                ? $storage->getTotalSpace()
91
                : null,
92 1
            Cache::STATS_MEMORY_AVAILIABLE => $storage instanceof AvailableSpaceCapableInterface
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\Common\Cache\Ca...STATS_MEMORY_AVAILIABLE has been deprecated.

This class constant has been deprecated.

Loading history...
93 1
                ? $storage->getAvailableSpace()
94
                : null,
95
        ];
96
    }
97
}
98