Completed
Pull Request — 11.x (#154)
by Tim
05:15
created

CacheAdapterTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 109
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
isCached() 0 1 ?
notCached() 0 1 ?
toCache() 0 1 ?
fromCache() 0 1 ?
A raiseCounter() 0 18 2
A mergeAttributesRecursive() 0 23 3
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 2019 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
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 2019 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
30
 * @link      http://www.techdivision.com
31
 * @see       \TechDivision\Import\Cache\GenericCacheAdapter
32
 */
33
trait CacheAdapterTrait
34
{
35
36
    /**
37
     * Query whether or not a cache value for the passed cache key is available.
38
     *
39
     * @param string $key The cache key to query for
40
     *
41
     * @return boolean TRUE if the a value is available, else FALSE
42
     */
43
    abstract public function isCached($key);
44
45
    /**
46
     * Inversion of the isCached() method.
47
     *
48
     * @param string $key The cache key to query for
49
     *
50
     * @return boolean TRUE if the value is not available, else FALSE
51
     */
52
    abstract public function notCached($key);
53
54
    /**
55
     * Add the passed item to the cache.
56
     *
57
     * @param string  $key        The cache key to use
58
     * @param mixed   $value      The value that has to be cached
59
     * @param array   $references An array with references to add
60
     * @param array   $tags       An array with tags to add
61
     * @param boolean $override   Flag that allows to override an exising cache entry
62
     * @param integer $time       The TTL in seconds for the passed item
63
     *
64
     * @return void
65
     */
66
    abstract public function toCache($key, $value, array $references = array(), array $tags = array(), $override = false, $time = null);
67
68
    /**
69
     * Returns a new cache item for the passed key
70
     *
71
     * @param string $key The cache key to return the item for
72
     *
73
     * @return mixed The value for the passed key
74
     */
75
    abstract public function fromCache($key);
76
77
    /**
78
     * Raises the value for the attribute with the passed key by one.
79
     *
80
     * @param mixed $key         The key of the attribute to raise the value for
81
     * @param mixed $counterName The name of the counter to raise
82
     *
83
     * @return integer The counter's new value
84
     */
85
    public function raiseCounter($key, $counterName)
86
    {
87
88
        // initialize the counter
89
        $counter = 0;
90
91
        // raise/initialize the value
92
        if ($this->isCached($key)) {
93
            $value = $this->fromCache($key);
94
            $counter = $value[$counterName];
95
        }
96
97
        // set the counter value back to the cache item/cache
98
        $this->toCache($key, array($counterName => ++$counter), array(), array(), true);
99
100
        // return the new value
101
        return $counter;
102
    }
103
104
    /**
105
     * This method merges the passed attributes with an array that
106
     * has already been added under the passed key.
107
     *
108
     * If no value will be found under the passed key, the attributes
109
     * will simply be registered.
110
     *
111
     * @param mixed $key        The key of the attributes that has to be merged with the passed ones
112
     * @param array $attributes The attributes that has to be merged with the exising ones
113
     *
114
     * @return void
115
     * @throws \Exception Is thrown, if the already registered value is no array
116
     * @link http://php.net/array_replace_recursive
117
     */
118
    public function mergeAttributesRecursive($key, array $attributes)
119
    {
120
121
        // if the key not exists, simply add the new attributes
122
        if ($this->notCached($key)) {
123
            $this->toCache($key, $attributes);
124
            return;
125
        }
126
127
        // if the key exists and the value is an array, merge it with the passed array
128
        if (is_array($value = $this->fromCache($key))) {
129
            $this->toCache($key, array_replace_recursive($value, $attributes), array(), array(), true);
130
            return;
131
        }
132
133
        // throw an exception if the key exists, but the found value is not of type array
134
        throw new \Exception(
135
            sprintf(
136
                'Can\'t merge attributes, because value for key "%s" already exists, but is not of type array',
137
                $key
138
            )
139
        );
140
    }
141
}
142