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

LaminasStorageCache::doFlush()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.032
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