CacheAdapterTrait::raiseCounter()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 21
ccs 0
cts 8
cp 0
rs 10
cc 4
nc 3
nop 2
crap 20
1
<?php
2
3
/**
4
 * TechDivision\Import\Cache\Collection\CacheAdapterTrait
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2021 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-cache-collection
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Cache\Collection;
16
17
/**
18
 * Trait that provides custom cache adapter functionality.
19
 *
20
 * @author    Tim Wagner <[email protected]>
21
 * @copyright 2021 TechDivision GmbH <[email protected]>
22
 * @license   https://opensource.org/licenses/MIT
23
 * @link      https://github.com/techdivision/import-cache-collection
24
 * @link      http://www.techdivision.com
25
 */
26
trait CacheAdapterTrait
27
{
28
29
    /**
30
     * Query whether or not a cache value for the passed cache key is available.
31
     *
32
     * @param string $key The cache key to query for
33
     *
34
     * @return boolean TRUE if the a value is available, else FALSE
35
     */
36
    abstract public function isCached($key);
37
38
    /**
39
     * Inversion of the isCached() method.
40
     *
41
     * @param string $key The cache key to query for
42
     *
43
     * @return boolean TRUE if the value is not available, else FALSE
44
     */
45
    abstract public function notCached($key);
46
47
    /**
48
     * Add the passed item to the cache.
49
     *
50
     * @param string  $key        The cache key to use
51
     * @param mixed   $value      The value that has to be cached
52
     * @param array   $references An array with references to add
53
     * @param array   $tags       An array with tags to add
54
     * @param boolean $override   Flag that allows to override an exising cache entry
55
     * @param integer $time       The TTL in seconds for the passed item
56
     *
57
     * @return void
58
     */
59
    abstract public function toCache($key, $value, array $references = array(), array $tags = array(), $override = true, $time = null);
60
61
    /**
62
     * Returns a new cache item for the passed key
63
     *
64
     * @param string $key The cache key to return the item for
65
     *
66
     * @return mixed The value for the passed key
67
     */
68
    abstract public function fromCache($key);
69
70
    /**
71
     * Raises the value for the attribute with the passed key by one.
72
     *
73
     * @param mixed $key         The key of the attribute to raise the value for
74
     * @param mixed $counterName The name of the counter to raise
75
     *
76
     * @return integer The counter's new value
77
     */
78
    public function raiseCounter($key, $counterName)
79
    {
80
81
        // initialize the counter
82
        $counter = 0;
83
84
        // raise/initialize the value
85
        if ($this->isCached($key)) {
86
            // try to load the array with the counters from the cache
87
            $value = $this->fromCache($key);
88
            // query whether or not a counter is available and try to load it
89
            if (is_array($value) && isset($value[$counterName])) {
90
                $counter = $value[$counterName];
91
            }
92
        }
93
94
        // set the counter value back to the cache item/cache
95
        $this->mergeAttributesRecursive($key, array($counterName => ++$counter));
96
97
        // return the new value
98
        return $counter;
99
    }
100
101
    /**
102
     * Lowers the value for the attribute with the passed key by one.
103
     *
104
     * @param mixed $key         The key of the attribute to lower the value for
105
     * @param mixed $counterName The name of the counter to lower
106
     *
107
     * @return integer The counter's new value
108
     */
109
    public function lowerCounter($key, $counterName)
110
    {
111
112
        // initialize the counter
113
        $counter = 0;
114
115
        // lower/initialize the value
116
        if ($this->isCached($key)) {
117
            // try to load the array with the counters from the cache
118
            $value = $this->fromCache($key);
119
            // query whether or not a counter is available and try to load it
120
            if (is_array($value) && isset($value[$counterName])) {
121
                $counter = $value[$counterName];
122
            }
123
        }
124
125
        // set the counter value back to the cache item/cache
126
        $this->mergeAttributesRecursive($key, array($counterName => --$counter));
127
128
        // return the new value
129
        return $counter;
130
    }
131
132
    /**
133
     * This method merges the passed attributes with an array that
134
     * has already been added under the passed key.
135
     *
136
     * If no value will be found under the passed key, the attributes
137
     * will simply be registered.
138
     *
139
     * @param mixed $key        The key of the attributes that has to be merged with the passed ones
140
     * @param array $attributes The attributes that has to be merged with the exising ones
141
     *
142
     * @return void
143
     * @throws \Exception Is thrown, if the already registered value is no array
144
     * @link http://php.net/array_replace_recursive
145
     */
146
    public function mergeAttributesRecursive($key, array $attributes)
147
    {
148
149
        // if the key not exists, simply add the new attributes
150
        if ($this->notCached($key)) {
151
            $this->toCache($key, $attributes);
152
            return;
153
        }
154
155
        // if the key exists and the value is an array, merge it with the passed array
156
        if (is_array($value = $this->fromCache($key))) {
157
            $this->toCache($key, array_replace_recursive($value, $attributes), array(), array(), true);
158
            return;
159
        }
160
161
        // throw an exception if the key exists, but the found value is not of type array
162
        throw new \Exception(
163
            sprintf(
164
                'Can\'t merge attributes, because value for key "%s" already exists, but is not of type array',
165
                $key
166
            )
167
        );
168
    }
169
}
170