Completed
Push — 14.x ( 855d13...227841 )
by Tim
02:29
created

CacheKeyUtil::prefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * TechDivision\Import\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 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\Utils;
22
23
use TechDivision\Import\ConfigurationInterface;
24
25
/**
26
 * A utility class to create cache keys.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2019 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
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\ConfigurationInterface $configuration The configuration instance
55
     */
56 20
    public function __construct(ConfigurationInterface $configuration)
57
    {
58 20
        $this->prefix = $configuration->getSerial();
59 20
    }
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
     *
66
     * @return string The generated cache key
67
     * @throws \Exception Is thrown if the passed data is not supported to create a cache key from
68
     */
69 20
    public function cacheKey($data)
70
    {
71
72
        // return the cache key for scalar data
73 20
        if (is_scalar($data)) {
74 20
            return $this->prefix($this->scalarKey($data));
75
        }
76
77
        // return the cache key for an array
78
        if (is_array($data)) {
79
            return $this->prefix($this->arrayKey($data));
80
        }
81
82
        // return the cache key for an object
83
        if (is_object($data)) {
84
            return $this->prefix($this->objectKey($data));
85
        }
86
87
        // throw an exception if the passed data type is NOT supported
88
        throw new \Exception(
89
            sprintf('Found not supported data type "%s" when preparing cache key', gettype($data))
90
        );
91
    }
92
93
    /**
94
     * Prefixes the cache key, e. g. with the products serial.
95
     *
96
     * @param string $cacheKey The cache key to prefix
97
     *
98
     * @return string The prefixed cache key
99
     */
100 20
    protected function prefix($cacheKey)
101
    {
102 20
        return $this->prefix . $cacheKey;
103
    }
104
105
    /**
106
     * Creates a cache key for a scalar values.
107
     *
108
     * @param mixed $data The scalar value to c
109
     *
110
     * @return string
111
     */
112 20
    protected function scalarKey($data)
113
    {
114 20
        return CacheKeyUtil::SEPARATOR . $data;
115
    }
116
117
    /**
118
     * Creates a cache key from the passed object.
119
     *
120
     * The object MUST implement the __toString method, else an exception will be thrown.
121
     *
122
     * @param object $data The object to create the cache key for
123
     *
124
     * @return string The cache key from the object's __toString method
125
     * @throws \Exception Is thrown, if the object doesn't implement the __toString method
126
     */
127
    protected function objectKey($data)
128
    {
129
130
        // query whether or not the object implements the __toString method
131
        if (method_exists($data, '__toString')) {
132
            return CacheKeyUtil::SEPARATOR . $data->__toString();
133
        }
134
135
        // throw an exception if not
136
        throw new \Exception(
137
            sprintf('Class "%s" doesn\'t implement necessary method "__toString" to create a cache key', get_class($data))
138
        );
139
    }
140
141
    /**
142
     * Creates a cache key from the passed array.
143
     *
144
     * @param array $data The array to create the cache key for
145
     *
146
     * @return string The cache key created from the array's key => value pairs
147
     * @throws \Exception Is thrown, if the array contains unsupported values or the array is empty
148
     */
149
    protected function arrayKey(array $data)
150
    {
151
152
        // query whether or not the array contains data
153
        if (sizeof($data) > 0) {
154
            // intialize the cache key
155
            $cacheKey = '';
156
            // iterate over the array's values, prepare and finally return the cache key
157
            foreach ($data as $key => $value) {
158
                if (is_scalar($value)) {
159
                    $cacheKey .= $this->scalarKey($key) . $this->scalarKey($value);
160
                } elseif (is_array($value)) {
161
                    $cacheKey .= $this->scalarKey($key) . $this->arrayKey($value);
162
                } elseif (is_object($data)) {
163
                    $cacheKey .= $this->scalarKey($key) . $this->objectKey($value);
164
                } else {
165
                    throw new \Exception(
166
                        sprintf('Found not supported data type "%s" for key "%s" in array when preparing cache key', $key, gettype($value))
167
                    );
168
                }
169
            }
170
171
            // return the cache key
172
            return $cacheKey;
173
        }
174
175
        // throw an exception if the array contains no data
176
        throw new \Exception('Array to create the cache key from contains no data');
177
    }
178
}
179