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

CacheKeyUtil   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 145
ccs 0
cts 56
cp 0
rs 10
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A objectKey() 0 11 2
A __construct() 0 3 1
A cacheKey() 0 21 4
A arrayKey() 0 28 6
A prefix() 0 3 2
A scalarKey() 0 3 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Cache\Utils\CacheKeys
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\Utils;
22
23
use TechDivision\Import\Configuration\ConfigurationInterface;
24
25
/**
26
 * A utility class to create cache keys.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2021 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import-cache
32
 * @link      http://www.techdivision.com
33
 */
34
class CacheKeyUtil implements CacheKeyUtilInterface
35
{
36
37
    /**
38
     * The separator for cache key elements.
39
     *
40
     * @var string
41
     */
42
    const SEPARATOR = '-';
43
44
    /**
45
     * The prefix to use.
46
     *
47
     * @var string
48
     */
49
    protected $prefix;
50
51
    /**
52
     * Initializes the cache key util with the passed configuration.
53
     *
54
     * @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The configuration instance
55
     */
56
    public function __construct(ConfigurationInterface $configuration)
57
    {
58
        $this->prefix = $configuration->getSerial();
59
    }
60
61
    /**
62
     * Creates a unique cache key from the passed data.
63
     *
64
     * @param mixed   $data      The date to create the cache key from
65
     * @param boolean $usePrefix Flag to signal using the prefix or not
66
     *
67
     * @return string The generated cache key
68
     * @throws \Exception Is thrown if the passed data is not supported to create a cache key from
69
     */
70
    public function cacheKey($data, $usePrefix = true)
71
    {
72
73
        // return the cache key for scalar data
74
        if (is_scalar($data)) {
75
            return $this->prefix($this->scalarKey($data), $usePrefix);
76
        }
77
78
        // return the cache key for an array
79
        if (is_array($data)) {
80
            return $this->prefix($this->arrayKey($data), $usePrefix);
81
        }
82
83
        // return the cache key for an object
84
        if (is_object($data)) {
85
            return $this->prefix($this->objectKey($data), $usePrefix);
86
        }
87
88
        // throw an exception if the passed data type is NOT supported
89
        throw new \Exception(
90
            sprintf('Found not supported data type "%s" when preparing cache key', gettype($data))
91
        );
92
    }
93
94
    /**
95
     * Prefixes the cache key, e. g. with the products serial.
96
     *
97
     * @param string  $cacheKey  The cache key to prefix
98
     * @param boolean $usePrefix Flag to signal using the prefix or not
99
     *
100
     * @return string The prefixed cache key
101
     */
102
    protected function prefix($cacheKey, $usePrefix)
103
    {
104
        return $usePrefix ? $this->prefix . $cacheKey : ltrim($cacheKey, CacheKeyUtil::SEPARATOR);
105
    }
106
107
    /**
108
     * Creates a cache key for a scalar values.
109
     *
110
     * @param mixed $data The scalar value to c
111
     *
112
     * @return string
113
     */
114
    protected function scalarKey($data)
115
    {
116
        return CacheKeyUtil::SEPARATOR . $data;
117
    }
118
119
    /**
120
     * Creates a cache key from the passed object.
121
     *
122
     * The object MUST implement the __toString method, else an exception will be thrown.
123
     *
124
     * @param object $data The object to create the cache key for
125
     *
126
     * @return string The cache key from the object's __toString method
127
     * @throws \Exception Is thrown, if the object doesn't implement the __toString method
128
     */
129
    protected function objectKey($data)
130
    {
131
132
        // query whether or not the object implements the __toString method
133
        if (method_exists($data, '__toString')) {
134
            return CacheKeyUtil::SEPARATOR . $data->__toString();
135
        }
136
137
        // throw an exception if not
138
        throw new \Exception(
139
            sprintf('Class "%s" doesn\'t implement necessary method "__toString" to create a cache key', get_class($data))
140
        );
141
    }
142
143
    /**
144
     * Creates a cache key from the passed array.
145
     *
146
     * @param array $data The array to create the cache key for
147
     *
148
     * @return string The cache key created from the array's key => value pairs
149
     * @throws \Exception Is thrown, if the array contains unsupported values or the array is empty
150
     */
151
    protected function arrayKey(array $data)
152
    {
153
154
        // query whether or not the array contains data
155
        if (sizeof($data) > 0) {
156
            // intialize the cache key
157
            $cacheKey = '';
158
            // iterate over the array's values, prepare and finally return the cache key
159
            foreach ($data as $key => $value) {
160
                if (is_scalar($value)) {
161
                    $cacheKey .= $this->scalarKey($key) . $this->scalarKey($value);
162
                } elseif (is_array($value)) {
163
                    $cacheKey .= $this->scalarKey($key) . $this->arrayKey($value);
164
                } elseif (is_object($data)) {
165
                    $cacheKey .= $this->scalarKey($key) . $this->objectKey($value);
166
                } else {
167
                    throw new \Exception(
168
                        sprintf('Found not supported data type "%s" for key "%s" in array when preparing cache key', $key, gettype($value))
169
                    );
170
                }
171
            }
172
173
            // return the cache key
174
            return $cacheKey;
175
        }
176
177
        // throw an exception if the array contains no data
178
        throw new \Exception('Array to create the cache key from contains no data');
179
    }
180
}
181