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

CachedConfigCollection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 82
ccs 0
cts 27
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A set() 0 18 3
A get() 0 10 3
A exists() 0 4 1
A delete() 0 4 1
A __destruct() 0 4 1
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