Completed
Push — 15.x ( b45baa...4ca9b4 )
by Tim
02:13
created

CacheKeys::getCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 2016 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
/**
24
 * A utility class that contains the cache keys.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 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
 */
32
class CacheKeys extends \ArrayObject implements CacheKeysInterface
33
{
34
35
    /**
36
     * The cache key for import status.
37
     *
38
     * @var string
39
     */
40
    const STATUS = 'status';
41
42
    /**
43
     * The cache key for references.
44
     *
45
     * @var string
46
     */
47
    const REFERENCES = 'references';
48
49
    /**
50
     * The cache key for artefacts.
51
     *
52
     * @var string
53
     */
54
    const ARTEFACTS = 'artefacts';
55
56
    /**
57
     * The cache key for EAV attribute option values.
58
     *
59
     * @var string
60
     */
61
    const EAV_ATTRIBUTE_OPTION_VALUE = 'eav_attribute_option_value';
62
63
    /**
64
     * The instance cache key.
65
     *
66
     * @var string
67
     */
68
    protected $cacheKey;
69
70
    /**
71
     * Initializes the instance with the passed cache key.
72
     *
73
     * @param string $cacheKey  The cache key use
74
     * @param array  $cacheKeys Additional cache keys
75
     */
76
    public function __construct($cacheKey, array $cacheKeys = array())
77
    {
78
79
        // merge the passed cache keys with the one from this class
80
        $mergedCacheKeys = array_merge(
81
            array(
82
                CacheKeys::STATUS,
83
                CacheKeys::REFERENCES,
84
                CacheKeys::ARTEFACTS,
85
                CacheKeys::EAV_ATTRIBUTE_OPTION_VALUE
86
            ),
87
            $cacheKeys
88
        );
89
90
        // pass them to the parent instance
91
        parent::__construct($mergedCacheKeys);
92
93
        // query whether or not we've a valid cache key
94
        if ($this->isCacheKey($cacheKey)) {
95
            $this->cacheKey = $cacheKey;
96
        } else {
97
            throw new \InvalidArgumentException(sprintf('Found invalid cache key "%s"', $cacheKey));
98
        }
99
    }
100
101
    /**
102
     * Factory method to create a new cache key instance.
103
     *
104
     * @param string $cacheKey The cache key to use
105
     *
106
     * @return \TechDivision\Import\Utils\CacheKeys The cache key instance
107
     */
108
    public static function get($cacheKey)
109
    {
110
        return new static($cacheKey);
111
    }
112
113
    /**
114
     * Query whether or not the passed cache key is valid.
115
     *
116
     * @param string $cacheKey The cache key to query for
117
     *
118
     * @return boolean TRUE if the cache key is valid, else FALSE
119
     */
120
    public function isCacheKey($cacheKey)
121
    {
122
        return in_array($cacheKey, (array) $this);
123
    }
124
125
    /**
126
     * Returns the cache key of the actual instance.
127
     *
128
     * @return string The cache key
129
     */
130
    public function getCacheKey()
131
    {
132
        return $this->cacheKey;
133
    }
134
}
135