Passed
Push — develop ( d6d0d2...d144b2 )
by Kevin
02:38
created

Manager::getBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Magium\Configuration\Manager;
4
5
use Magium\Configuration\Config\Builder;
6
use Magium\Configuration\Config\Config;
7
use Zend\Cache\Storage\StorageInterface;
8
9
class Manager
10
{
11
12
    protected $config = [];
13
14
    protected $cache;
15
    protected $localCache;
16
    protected $builder;
17
    protected $configurationLocation;
18
19
    public function __construct(
20
        StorageInterface $cache,
21
        Builder $builder,
22
        StorageInterface $localCache = null
23
    )
24
    {
25
        $this->cache = $cache;
26
        $this->builder = $builder;
27
        $this->localCache = $localCache;
28
     }
29
30
     public function getBuilder()
31
     {
32
         return $this->builder;
33
     }
34
35
    /**
36
     * @param string $context The (configurable) context for the needed configuration object
37
     * @return Config
38
     * @throws NoConfigurationException
39
     */
40
41
    public function getConfiguration($context = Config::CONTEXT_DEFAULT, $storeScopeLocally = false)
42
    {
43
        $key = 'current_cache_object_' . $context;
44
        if (isset($this->config[$key]) && $this->config[$key] instanceof Config) {
45
            return $this->config[$key];
46
        }
47
48
        // By default we will get the config location from the remote server for each request.  But we can override that.
49
        if ($this->localCache instanceof StorageInterface && $storeScopeLocally) {
50
            // If a local cache is defined we check there for the current scope key first
51
            $currentConfigItem = $this->localCache->getItem($key);
52
            // if we don't find the scope key we check the global cache
53
            if (!$currentConfigItem) {
54
                $currentConfigItem = $this->cache->getItem($key);
55
                // If the primary cache gives us a value we store it locally
56
                if ($currentConfigItem) {
57
                    $this->localCache->setItem($key, $currentConfigItem);
58
                }
59
            }
60
        } else {
61
            // Local cache is not configured, so we get the current config cache key from the global cache.
62
            $currentConfigItem = $this->cache->getItem($key);
63
        }
64
65
        $config = null;
66
        if ($this->localCache instanceof StorageInterface) {
67
            // First we check the local cache for the configuration object if it exists.
68
            $config = $this->localCache->getItem($currentConfigItem);
69
        }
70
71
        // Either way, if the config is null we check the global cache
72
        if ($config === null) {
73
            $config = $this->cache->getItem($currentConfigItem);
74
            if ($config !== null) {
75
                if ($this->localCache instanceof StorageInterface) {
76
                    $this->localCache->setItem($currentConfigItem, $config);
77
                }
78
             }
79
        }
80
81
        if ($config) {
82
            $config = new Config($config);
83
            $this->config[$key] = $config;
84
            return $this->config[$key];
85
        }
86
        $config = $this->builder->build($context);
87
        $this->config[$key] = $config;
88
        return $config;
89
    }
90
91
}
92