FileBackend   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 167
ccs 55
cts 55
cp 1
rs 10
wmc 22

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setEventDispatcher() 0 3 1
A dispatch() 0 4 2
A setMultiple() 0 3 1
A __construct() 0 3 1
A removeBin() 0 3 1
A invalidateAll() 0 3 1
A deleteAll() 0 3 1
A get() 0 21 4
A garbageCollection() 0 3 1
A deleteMultiple() 0 3 1
A delete() 0 10 2
A invalidate() 0 3 1
A set() 0 12 3
A invalidateMultiple() 0 3 1
A getMultiple() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the ekino Drupal Debug project.
7
 *
8
 * (c) ekino
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\Drupal\Debug\Cache;
15
16
use Drupal\Core\Cache\Cache;
17
use Drupal\Core\Cache\CacheBackendInterface;
18
use Ekino\Drupal\Debug\Cache\Event\CacheNotFreshEvent;
19
use Ekino\Drupal\Debug\Cache\Event\FileBackendEvents;
20
use Ekino\Drupal\Debug\Exception\NotImplementedException;
21
use Symfony\Component\EventDispatcher\Event;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
24
class FileBackend implements CacheBackendInterface
25
{
26
    /**
27
     * @var FileCache
28
     */
29
    private $fileCache;
30
31
    /**
32
     * @var EventDispatcherInterface|null
33
     */
34
    private $eventDispatcher;
35
36
    /**
37
     * @param FileCache $fileCache
38
     */
39 21
    public function __construct(FileCache $fileCache)
40
    {
41 21
        $this->fileCache = $fileCache;
42 21
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 5
    public function get($cid, $allow_invalid = false)
48
    {
49 5
        if ($allow_invalid) {
50 1
            throw new NotImplementedException('$allow_invalid with true value is not implemented.');
51
        }
52
53 4
        if (!$this->fileCache->isFresh()) {
54 2
            $this->dispatch(FileBackendEvents::ON_CACHE_NOT_FRESH, new CacheNotFreshEvent($this->fileCache));
55
56 2
            return false;
57
        }
58
59 2
        $data = $this->fileCache->getData();
60 2
        if (!\array_key_exists($cid, $data)) {
61 1
            return false;
62
        }
63
64 1
        $object = new \stdClass();
65 1
        $object->data = $data[$cid];
66
67 1
        return $object;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 1
    public function getMultiple(&$cids, $allow_invalid = false): array
74
    {
75 1
        throw new NotImplementedException('The getMultiple() method is not implemented.');
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 3
    public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array()): void
82
    {
83 3
        if (Cache::PERMANENT !== $expire) {
84 1
            throw new NotImplementedException(\sprintf('$expire argument with "%s" value is not implemented.', $expire));
85
        }
86
87 2
        if (!empty($tags)) {
88 1
            throw new NotImplementedException('Non empty $tags argument is not implemented.');
89
        }
90
91 1
        $this->fileCache->write(array(
92 1
            $cid => $data,
93
        ));
94 1
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 1
    public function setMultiple(array $items): void
100
    {
101 1
        throw new NotImplementedException('The setMultiple() method is not implemented.');
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 3
    public function delete($cid): void
108
    {
109 3
        $data = $this->fileCache->get();
110 3
        if (!\is_array($data)) {
111 1
            return;
112
        }
113
114 2
        unset($data['data'][$cid]);
115
116 2
        $this->fileCache->write($data['data']);
117 2
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 1
    public function deleteMultiple(array $cids): void
123
    {
124 1
        throw new NotImplementedException('The deleteMultiple() method is not implemented.');
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 1
    public function deleteAll(): void
131
    {
132 1
        $this->fileCache->invalidate();
133 1
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 1
    public function invalidate($cid): void
139
    {
140 1
        throw new NotImplementedException('The invalidate() method is not implemented.');
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 1
    public function invalidateMultiple(array $cids): void
147
    {
148 1
        throw new NotImplementedException('The invalidateMultiple() method is not implemented.');
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 1
    public function invalidateAll(): void
155
    {
156 1
        throw new NotImplementedException('The invalidateAll() method is not implemented.');
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 1
    public function garbageCollection(): void
163
    {
164 1
        throw new NotImplementedException('The garbageCollection() method is not implemented.');
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 1
    public function removeBin(): void
171
    {
172 1
        throw new NotImplementedException('The removeBin() method is not implemented.');
173
    }
174
175
    /**
176
     * @param EventDispatcherInterface $eventDispatcher
177
     */
178 2
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
179
    {
180 2
        $this->eventDispatcher = $eventDispatcher;
181 2
    }
182
183
    /**
184
     * @param string     $eventName
185
     * @param Event|null $event
186
     */
187 2
    private function dispatch(string $eventName, ?Event $event): void
188
    {
189 2
        if ($this->eventDispatcher instanceof EventDispatcherInterface) {
190 1
            $this->eventDispatcher->dispatch($eventName, $event);
191
        }
192 2
    }
193
}
194