Completed
Pull Request — develop (#61)
by Kevin
09:23
created

MagiumConfigurationFactory::getLocalCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Magium\Configuration;
4
5
use Magium\Configuration\Config\BuilderFactory;
6
use Magium\Configuration\Config\BuilderFactoryInterface;
7
use Magium\Configuration\Config\BuilderInterface;
8
use Magium\Configuration\Config\Repository\ConfigurationRepository;
9
use Magium\Configuration\Config\Context;
10
use Magium\Configuration\Config\MissingConfigurationException;
11
use Magium\Configuration\File\Context\AbstractContextConfigurationFile;
12
use Magium\Configuration\Manager\CacheFactory;
13
use Magium\Configuration\Manager\Manager;
14
use Magium\Configuration\Manager\ManagerInterface;
15
16
class MagiumConfigurationFactory implements MagiumConfigurationFactoryInterface
17
{
18
19
    protected $file;
20
    protected $xml;
21
22
    protected $manager;
23
    protected $builder;
24
    protected $baseDir;
25
    protected $contextFile;
26
    protected $builderFactory;
27
    protected $context = ConfigurationRepository::CONTEXT_DEFAULT;
28
29
    protected static $me;
30
31 16
    public function __construct($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT, $cwd = __DIR__)
32
    {
33 16
        self::$me = $this;
34 16
        if ($context instanceof Context) {
35 1
            $context = $context->getContext();
36
        }
37 16
        $this->context = $context;
38 16
        if (!$magiumConfigurationFile) {
39 7
            $baseDir = realpath(DIRECTORY_SEPARATOR);
40 7
            while ($cwd && $cwd != $baseDir && file_exists($cwd)) {
41 7
                $checkFile = $cwd . DIRECTORY_SEPARATOR . 'magium-configuration.xml';
42 7
                if (file_exists($checkFile)) {
43 6
                    $magiumConfigurationFile = $checkFile;
44 6
                    break;
45
                }
46 7
                $lastPos = strrpos($cwd, DIRECTORY_SEPARATOR);
47 7
                $cwd = substr($cwd, 0, $lastPos);
48
            }
49
        }
50
51 16
        if (file_exists($magiumConfigurationFile)) {
52 15
            $this->file = realpath($magiumConfigurationFile);
53
        } else {
54 1
            throw new InvalidConfigurationFileException('Unable to file configuration file: ' . $magiumConfigurationFile);
55
        }
56 15
        $this->baseDir = dirname($this->file);
57 15
        chdir($this->baseDir);
58 15
        $this->xml = simplexml_load_file($magiumConfigurationFile);
59 15
    }
60
61 2
    protected static function getInstance($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT)
0 ignored issues
show
Unused Code introduced by
The parameter $magiumConfigurationFile is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63 2
        if (!self::$me instanceof self) {
64
            new self($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT);
65
        }
66 2
        return self::$me;
67
    }
68
69
    public static function configurationFactory($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT)
70
    {
71
        $me = self::getInstance($magiumConfigurationFile, $context);
72
        return $me->getConfiguration($context);
73
    }
74
75 1
    public static function builderFactory($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT)
76
    {
77 1
        $me = self::getInstance($magiumConfigurationFile, $context);
78 1
        return $me->getBuilder();
79
    }
80
81 1
    public static function managerFactory($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT)
82
    {
83 1
        $me = self::getInstance($magiumConfigurationFile, $context);
84 1
        return $me->getManager();
85
    }
86
87
    public function getConfiguration($context = ConfigurationRepository::CONTEXT_DEFAULT)
88
    {
89
        return $this->getManager()->getConfiguration($context);
90
    }
91
92
    public function setContext($context)
93
    {
94
        $this->context = $context;
95
    }
96
97 5
    protected function buildContextFile()
98
    {
99 5
        chdir($this->baseDir);
100 5
        $contextFileCheck = (string)$this->xml->contextConfigurationFile['file'];
101 5
        $contextFileType = (string)$this->xml->contextConfigurationFile['type'];
102 5
        $contextFile = realpath($contextFileCheck);
103 5
        if (!$contextFile) {
104 1
            throw new MissingConfigurationException('Unable to find context file: ' . $contextFileCheck);
105
        }
106 4
        $class = 'Magium\Configuration\File\Context\\' . ucfirst($contextFileType) . 'File';
107 4
        $reflectionClass = new \ReflectionClass($class);
108 4
        if ($reflectionClass->isSubclassOf(AbstractContextConfigurationFile::class)) {
109 3
            $instance = $reflectionClass->newInstance($contextFile);
110 3
            if ($instance instanceof AbstractContextConfigurationFile) {
111 3
                return $instance;
112
            }
113
        }
114 1
        throw new InvalidConfigurationException('Unable to load context configuration file: ' . $contextFileCheck);
115
    }
116
117 5
    public function getContextFile()
118
    {
119 5
        if (!$this->contextFile instanceof AbstractContextConfigurationFile) {
120 5
            $this->contextFile = $this->buildContextFile();
121
        }
122 3
        return $this->contextFile;
123
    }
124
125 2
    public function validateConfigurationFile()
126
    {
127 2
        $result = false;
128
        try {
129 2
            $doc = new \DOMDocument();
130 2
            $doc->load($this->file);
131 2
            $result = $doc->schemaValidate(__DIR__ . '/../assets/magium-configuration.xsd');
132 1
        } catch (\Exception $e) {
133
            // $result value is already set
134
        }
135 2
        return $result;
136
    }
137
138
    /**
139
     * Retrieves an instance of the cache based off of the XML cache configuration
140
     *
141
     * @param \SimpleXMLElement $element
142
     * @return \Zend\Cache\Storage\StorageInterface
143
     */
144
145 3
    protected function getCache(\SimpleXMLElement $element)
146
    {
147 3
        $cacheFactory = new CacheFactory();
148 3
        return $cacheFactory->getCache($element);
149
    }
150
151 4
    public function getBuilderFactory()
152
    {
153 4
        if (!$this->builderFactory instanceof BuilderFactoryInterface) {
154 4
            $builderFactoryConfig = $this->xml->builderFactory;
155 4
            $class = (string)$builderFactoryConfig['class'];
156 4
            if (!$class) {
157 3
                $class = BuilderFactory::class; // das default
158
            }
159 4
            $reflection = new \ReflectionClass($class);
160 4
            if (!$reflection->implementsInterface(BuilderFactoryInterface::class)) {
161 1
                throw new InvalidConfigurationException($class . ' must implement ' . BuilderFactoryInterface::class);
162
            }
163 3
            $this->builderFactory = $reflection->newInstance(new \SplFileInfo($this->baseDir), $this->xml, $this->getContextFile());
164
        }
165 3
        return $this->builderFactory;
166
    }
167
168
    /**
169
     * @return BuilderInterface
170
     */
171
172 4
    public function getBuilder()
173
    {
174 4
        if (!$this->builder instanceof BuilderInterface) {
175 3
            $this->builder = $this->getBuilderFactory()->getBuilder();
176
        }
177 3
        return $this->builder;
178
    }
179
180 5
    protected function getRemoteCache()
181
    {
182 5
        $cacheConfig = $this->xml->cache;
183 5
        $globalAdapter = $this->getCache($cacheConfig);
184 5
        return $globalAdapter;
185
    }
186
187 4
    protected function getLocalCache()
188
    {
189 4
        $localCache = null;
190 4
        $localCacheConfig = $this->xml->localCache;
191 4
        if ($localCacheConfig) {
192 2
            $localCache = $this->getCache($localCacheConfig);
193
        }
194 4
        return $localCache;
195
    }
196
197 7
    public function getManager()
198
    {
199 7
        if (!$this->manager instanceof ManagerInterface) {
200 6
            $managerClass = Manager::class;
201 6
            if (isset($this->xml->manager['class'])) {
202 2
                $managerClass = (string)$this->xml->manager['class'];
203
            }
204 6
            $reflectionClass = new \ReflectionClass($managerClass);
205 6
            if ($managerClass == Manager::class) {
206
                // just a shortcut so I don't have to rewrite some complicated unit tests.  I'm just lazy.
207 4
                $this->manager = new Manager($this->getRemoteCache(), $this->getBuilder(), $this->getLocalCache());
208 3
                return $this->manager;
209
            }
210 2
            if (!$reflectionClass->implementsInterface(ManagerInterface::class)) {
211 1
                throw new InvalidConfigurationException('Manager class must implement ' . ManagerInterface::class);
212
            }
213 1
            $manager = $reflectionClass->newInstance();
214
            /* @var $manager ManagerInterface */
215 1
            $manager->setBuilder($this->getBuilder());
216 1
            $manager->setLocalCache($this->getLocalCache());
217 1
            $manager->setRemoteCache($this->getRemoteCache());
218 1
            $this->manager = $manager;
219
        }
220 2
        return $this->manager;
221
    }
222
223
}
224