Passed
Branch add-cache (b150cc)
by Michael
02:18
created

CachedConfigCollection::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace micmania1\config;
4
5
use Psr\Cache\CacheItemPoolInterface;
6
7
class CachedConfigCollection implements ConfigCollectionInterface
8
{
9
    /**
10
     * @var CacheItemPoolInterface
11
     */
12
    protected $pool;
13
14
    /**
15
     * @var boolean
16
     */
17
    protected $trackMetadata = false;
18
19
    /**
20
     * @param boolean $trackMetadata
21
     * @param CacheItemPoolInterface $pool
22
     */
23
    public function __construct(CacheItemPoolInterface $pool, $trackMetadata = false)
24
    {
25
        $this->pool = $pool;
26
        $this->trackMetadata = (bool) $trackMetadata;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function set($key, ConfigItemInterface $item)
33
    {
34
        // We use null as the key to return an empty cache item
35
        $cacheItem = $this->pool->getItem($key);
36
37
        // If we're tracking history and already have an existing record, we grab
38
        // that and set the new record.
39
        if($this->trackMetadata && $existing = $cacheItem->get()) {
40
            $existing->trackMetadata();
41
            $existing->set($item->getValue(), $item->getMetadata());
42
            $item = $existing;
43
        }
44
45
        $cacheItem->set($item);
46
47
        // We defer saving until later
48
        $this->pool->saveDeffered($cacheItem);
0 ignored issues
show
Bug introduced by
The method saveDeffered() does not exist on Psr\Cache\CacheItemPoolInterface. Did you maybe mean saveDeferred()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function get($key)
55
    {
56
        if(!$this->exists($key)) {
57
            return null;
58
        }
59
60
        $cacheItem = $this->pool->getItem($key);
61
62
        return $cacheItem->get() ?: null;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function exists($key)
69
    {
70
        return $this->pool->hasItem($key);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function delete($key)
77
    {
78
        $this->pool->deleteItem($key);
79
    }
80
81
    /**
82
     * Commits the cache
83
     */
84
    public function __destruct()
85
    {
86
        $this->pool->commit();
87
    }
88
}
89