Completed
Push — master ( 753c87...0fd538 )
by Tom
01:44 queued 10s
created

LaminasStorageCache::doFlush()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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