Manager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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