Test Failed
Pull Request — develop (#19)
by Kevin
02:51
created

Manager::getContextCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Magium\Configuration\Manager;
4
5
use Magium\Configuration\Config\Builder;
6
use Magium\Configuration\Config\BuilderInterface;
7
use Magium\Configuration\Config\Config;
8
use Zend\Cache\Storage\StorageInterface;
9
10
class Manager implements ManagerInterface
11
{
12
13
    protected $config = [];
14
15
    protected $cache;
16
    protected $localCache;
17
    protected $builder;
18
    protected $configurationLocation;
19 12
    protected $hashAlgo;
20
21
    public function __construct(
22
        StorageInterface $cache = null,
23
        BuilderInterface $builder = null,
24
        StorageInterface $localCache = null,
25 12
        $hashAlgo = 'sha1'
26 12
    )
27 12
    {
28 12
        $this->cache = $cache;
29
        $this->builder = $builder;
30
        $this->localCache = $localCache;
31
        $this->hashAlgo = $hashAlgo;
32
    }
33
34
    public function getBuilder()
35
    {
36
        return $this->builder;
37
    }
38
39
    public function setLocalCache(StorageInterface $storage)
40
    {
41 9
        $this->localCache = $storage;
42
    }
43 9
44 9
    public function setRemoteCache(StorageInterface $storage)
45 1
    {
46
        $this->cache = $storage;
47
    }
48
49 9
    public function setBuilder(BuilderInterface $builder)
50
    {
51 2
        $this->builder = $builder;
52
    }
53 2
54 1
55
    public function getContextCacheKey($context = Config::CONTEXT_DEFAULT)
56 1
    {
57 2
        return strtoupper('current_cache_object_' . $context);
58
    }
59
60
    /**
61
     * @param string $context The (configurable) context for the needed configuration object
62 7
     * @return Config
63
     * @throws NoConfigurationException
64
     */
65 9
66 9
    public function getConfiguration($context = Config::CONTEXT_DEFAULT, $storeScopeLocally = false)
67
    {
68 4
        $key = $this->getContextCacheKey($context);
69
        if (isset($this->config[$key]) && $this->config[$key] instanceof Config) {
70
            return $this->config[$key];
71
        }
72 9
73 8
        // By default we will get the config location from the remote server for each request.  But we can override that.
74 8
        if ($this->localCache instanceof StorageInterface && $storeScopeLocally) {
75 4
            // If a local cache is defined we check there for the current scope key first
76 3
            $currentConfigItem = $this->localCache->getItem($key);
77
            // if we don't find the scope key we check the global cache
78
            if (!$currentConfigItem) {
79
                $currentConfigItem = $this->cache->getItem($key);
80
                // If the primary cache gives us a value we store it locally
81 9
                if ($currentConfigItem) {
82 5
                    $this->localCache->setItem($key, $currentConfigItem);
83 5
                }
84 5
            }
85
        } else {
86 4
            // Local cache is not configured, so we get the current config cache key from the global cache.
87 4
            $currentConfigItem = $this->cache->getItem($key);
88 4
        }
89
90
        $config = null;
91
        if ($this->localCache instanceof StorageInterface) {
92
            // First we check the local cache for the configuration object if it exists.
93
            $config = $this->localCache->getItem($currentConfigItem);
94
        }
95
96
        // Either way, if the config is null we check the global cache
97
        if ($config === null) {
98
            $config = $this->cache->getItem($currentConfigItem);
99
            if ($config !== null) {
100
                if ($this->localCache instanceof StorageInterface) {
101
                    $this->localCache->setItem($currentConfigItem, $config);
102
                }
103
                }
104
        }
105
106
        if ($config) {
107
            $config = new Config($config);
108
            $this->config[$key] = $config;
109
            return $this->config[$key];
110
        }
111
        $config = $this->builder->build($context);
112
        $this->storeConfigurationObject($config, $context);
113
        $this->config[$key] = $config;
114
        return $config;
115
    }
116
117
    public function storeConfigurationObject(Config $config, $context = Config::CONTEXT_DEFAULT)
118
    {
119
        $contextCacheKey = $this->getContextCacheKey($context);
120
        $previousConfigKey = $this->cache->getItem($contextCacheKey);
121
        $xml = $config->asXML();
122
        $configKey = hash_hmac($this->hashAlgo, $xml, '');
123
        $this->cache->addItem($configKey, $xml);
124
        $this->cache->setItem($contextCacheKey, $xml);
125
126
        if ($previousConfigKey) {
127
            $this->cache->removeItem($previousConfigKey);
128
        }
129
    }
130
131
}
132