Completed
Push — master ( b2ff41...3eced2 )
by Tim
14s
created

AbstractCachedRepository::cacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Repositories\EavAttributeOptionValueRepository
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\Repositories;
22
23
/**
24
 * Repository implementation to load EAV attribute option value data.
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
abstract class AbstractCachedRepository extends AbstractRepository implements CachedRepositoryInterface
33
{
34
35
    /**
36
     * The cache for the query results.
37
     *
38
     * @var array
39
     */
40
    protected $cache = array();
41
42
    /**
43
     * Prepares a unique cache key for the passed query name and params.
44
     *
45
     * @param string $queryName The query name to prepare the cache key for
46
     * @param array  $params    The query params
47
     *
48
     * @return string The prepared cache key
49
     */
50
    public function cacheKey($queryName, array $params)
51
    {
52
        return sprintf('%s-%s', $queryName, implode('-', $params));
53
    }
54
55
    /**
56
     * Query whether or not a cache value for the passed cache key is available.
57
     *
58
     * @param string $cacheKey The cache key to query for
59
     *
60
     * @return boolean TRUE if the a value is available, else FALSE
61
     */
62
    public function isCached($cacheKey)
63
    {
64
        return isset($this->cache[$cacheKey]);
65
    }
66
67
    /**
68
     * Inversion of the isCached() method.
69
     *
70
     * @param string $cacheKey The cache key to query for
71
     *
72
     * @return boolean TRUE if the value is not available, else FALSE
73
     */
74
    public function notCached($cacheKey)
75
    {
76
        return !$this->isCached($cacheKey);
77
    }
78
79
    /**
80
     * Add the passed value to the cache.
81
     *
82
     * @param string $cacheKey The cache key
83
     * @param mixed  $value    The value to cache
84
     *
85
     * @return void
86
     */
87
    public function toCache($cacheKey, $value)
88
    {
89
        $this->cache[$cacheKey] = $value;
90
    }
91
92
    /**
93
     * Query whether or not a value for the passed cache key exists or not. If yes, the value
94
     * will be returned, else an exception will be thrown.
95
     *
96
     * @param string $cacheKey The cache key to return the value for
97
     *
98
     * @return mixed The value for the passed cache key
99
     * @throws \Exception Is thrown, if no value is available
100
     */
101
    public function fromCache($cacheKey)
102
    {
103
104
        // query whether or not a value for the cache key is available
105
        if (isset($this->cache[$cacheKey])) {
106
            return $this->cache[$cacheKey];
107
        }
108
109
        // throw an exception if not
110
        throw new \Exception(sprintf('Can\'t find cached value for key "%s"', $cacheKey));
111
    }
112
}
113