Passed
Push — develop ( d84ab0...0ae1ad )
by Kevin
01:14
created

MagiumConfigurationFactory::getCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Magium\Configuration;
4
5
use Magium\Configuration\Config\Builder;
6
use Magium\Configuration\Config\BuilderFactory;
7
use Magium\Configuration\Config\BuilderFactoryInterface;
8
use Magium\Configuration\Config\InvalidConfigurationLocationException;
9
use Magium\Configuration\Config\MissingConfigurationException;
10
use Magium\Configuration\File\Context\AbstractContextConfigurationFile;
11
use Magium\Configuration\Manager\CacheFactory;
12
use Magium\Configuration\Manager\Manager;
13
14
class MagiumConfigurationFactory
15
{
16
    protected $file;
17
    protected $xml;
18
19
    protected $manager;
20
    protected $builder;
21
    protected $baseDir;
22
    protected $contextFile;
23
24 9
    public function __construct($magiumConfigurationFile = null)
25
    {
26 9
        if (!$magiumConfigurationFile) {
27 4
            $cwd = __DIR__;
28 4
            $baseDir = realpath(DIRECTORY_SEPARATOR);
29 4
            while ($cwd && $cwd != $baseDir && file_exists($cwd)) {
30 4
                $checkFile = $cwd . DIRECTORY_SEPARATOR . 'magium-configuration.xml';
31 4
                if (file_exists($checkFile)) {
32 3
                    $magiumConfigurationFile = $checkFile;
33 3
                    break;
34
                }
35 4
                $lastPos = strrpos($cwd, DIRECTORY_SEPARATOR);
36 4
                $cwd = substr($cwd, 0, $lastPos);
37
            }
38
        }
39
40 9
        if (file_exists($magiumConfigurationFile)) {
41 8
            $this->file = realpath($magiumConfigurationFile);
42
        } else {
43 1
            throw new InvalidConfigurationFileException('Unable to file configuration file: ' . $magiumConfigurationFile);
44
        }
45 8
        $this->baseDir = dirname($this->file);
46 8
        chdir($this->baseDir);
47 8
        $this->xml = simplexml_load_file($magiumConfigurationFile);
48 8
    }
49
50
    protected function buildContextFile()
51
    {
52
        chdir($this->baseDir);
53
        $contextFileCheck = (string)$this->xml->contextConfigurationFile['file'];
54
        $contextFileType = (string)$this->xml->contextConfigurationFile['type'];
55
        $contextFile = realpath($contextFileCheck);
56
        if (!$contextFile) {
57
            throw new MissingConfigurationException('Unable to find context file: ' . $contextFileCheck);
58
        }
59
        $class = 'Magium\Configuration\File\Context\\' . ucfirst($contextFileType) . 'File';
60
        $reflectionClass = new \ReflectionClass($class);
61
        if ($reflectionClass->isSubclassOf(AbstractContextConfigurationFile::class)) {
62
            $instance = $reflectionClass->newInstance($contextFile);
63
            if ($instance instanceof AbstractContextConfigurationFile) {
64
                return $instance;
65
            }
66
        }
67
        throw new InvalidConfigurationException('Unable to load context configuration file: ' . $contextFileCheck);
68
    }
69
70
    public function getContextFile()
71
    {
72
        if (!$this->contextFile instanceof AbstractContextConfigurationFile) {
73
            $this->contextFile = $this->buildContextFile();
74
        }
75
        return $this->contextFile;
76
    }
77
78 2
    public function validateConfigurationFile()
79
    {
80 2
        $result = false;
81
        try {
82 2
            $doc = new \DOMDocument();
83 2
            $doc->load($this->file);
84 2
            $result = $doc->schemaValidate(__DIR__ . '/../assets/magium-configuration.xsd');
85 1
        } catch (\Exception $e) {
86
            // $result value is already set
87
        }
88 2
        return $result;
89
    }
90
91
    /**
92
     * Retrieves an instance of the cache based off of the XML cache configuration
93
     *
94
     * @param \SimpleXMLElement $element
95
     * @return \Zend\Cache\Storage\StorageInterface
96
     */
97
98 2
    protected function getCache(\SimpleXMLElement $element)
99
    {
100 2
        $cacheFactory = new CacheFactory();
101 2
        return $cacheFactory->getCache($element);
102
    }
103
104
    /**
105
     * @return Builder
106
     */
107 2
    public function getBuilder()
108
    {
109 2
        if (!$this->builder instanceof Builder) {
110 2
            $builderFactoryConfig = $this->xml->builderFactory;
111 2
            $class = (string)$builderFactoryConfig['class'];
112 2
            if (!$class) {
113 2
                $class = BuilderFactory::class; // das default
114
            }
115 2
            $reflection = new \ReflectionClass($class);
116 2
            if (!$reflection->implementsInterface(BuilderFactoryInterface::class)) {
117
                throw new InvalidConfigurationException($class . ' must implement ' . BuilderFactoryInterface::class);
118
            }
119 2
            $builderFactory = $reflection->newInstance($this->xml);
120 2
            if (!$builderFactory instanceof BuilderFactoryInterface) {
121
                throw new InvalidConfigurationException(
122
                    sprintf(
123
                        'The builder factory %s must implement %s',
124
                        get_class($builderFactory),
125
                        BuilderFactoryInterface::class
126
                    )
127
                );
128
            }
129 2
            $this->builder = $builderFactory->getBuilder();
130
        }
131 1
        return $this->builder;
132
    }
133
134 4
    protected function getRemoteCache()
135
    {
136 4
        $cacheConfig = $this->xml->cache;
137 4
        $globalAdapter = $this->getCache($cacheConfig);
138 4
        return $globalAdapter;
139
    }
140
141 3
    protected function getLocalCache()
142
    {
143 3
        $localCache = null;
144 3
        $localCacheConfig = $this->xml->localCache;
145 3
        if ($localCacheConfig) {
146 1
            $localCache = $this->getCache($localCacheConfig);
147
        }
148 3
        return $localCache;
149
    }
150
151 4
    public function getManager()
152
    {
153 4
        if (!$this->manager instanceof Manager) {
154 4
            $this->manager = new Manager($this->getRemoteCache(), $this->getBuilder(), $this->getLocalCache());
155
        }
156 3
        return $this->manager;
157
    }
158
159
}
160