Completed
Pull Request — develop (#683)
by Tom
01:23
created

LaminasStorageCache   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 82
ccs 28
cts 29
cp 0.9655
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 11 2
A doGetStats() 0 18 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
use function assert;
14
15
/**
16
 * Bridge class that allows usage of a Laminas Cache Storage as a Doctrine Cache
17
 *
18
 * @link    http://www.doctrine-project.org/
19
 */
20
class LaminasStorageCache extends CacheProvider
21
{
22
    /** @var StorageInterface */
23
    protected $storage;
24
25 5
    public function __construct(StorageInterface $storage)
26
    {
27 5
        $this->storage = $storage;
28 5
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33 4
    protected function doFetch($id)
34
    {
35 4
        $hit = $this->storage->getItem($id);
36
37 4
        return $hit ?? false;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 4
    protected function doContains($id)
44
    {
45 4
        return $this->storage->hasItem($id);
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51 4
    protected function doSave($id, $data, $lifeTime = false)
52
    {
53
        // @todo check if lifetime can be set
54 4
        return $this->storage->setItem($id, $data);
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60 1
    protected function doDelete($id)
61
    {
62 1
        return $this->storage->removeItem($id);
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 1
    protected function doFlush()
69
    {
70 1
        if ($this->storage instanceof FlushableInterface) {
71 1
            $storage = $this->storage;
72 1
            assert($storage instanceof FlushableInterface);
73
74 1
            return $storage->flush();
75
        }
76
77
        return false;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83 1
    protected function doGetStats()
84
    {
85 1
        $storage = $this->storage;
86 1
        assert($storage instanceof TotalSpaceCapableInterface);
87 1
        assert($storage instanceof AvailableSpaceCapableInterface);
88
89
        return [
90 1
            Cache::STATS_HITS              => $this->storage->getMetadata(Cache::STATS_HITS),
91 1
            Cache::STATS_MISSES            => $this->storage->getMetadata(Cache::STATS_MISSES),
92 1
            Cache::STATS_UPTIME            => $this->storage->getMetadata(Cache::STATS_UPTIME),
93 1
            Cache::STATS_MEMORY_USAGE      => $storage instanceof TotalSpaceCapableInterface
94 1
                ? $storage->getTotalSpace()
95
                : null,
96 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...
97 1
                ? $storage->getAvailableSpace()
98
                : null,
99
        ];
100
    }
101
}
102