Passed
Push — develop ( 6eac47...79c28c )
by Kevin
05:14 queued 02:30
created

MagiumConfigurationFactory::getRemoteCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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