Passed
Push — master ( 0f35d9...62011e )
by Tim
03:47
created

CacheAdapterTrait::raiseCounter()   A

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