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

MagiumConfigurationFactory::getBuilder()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 7
nop 0
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\Manager\CacheFactory;
9
use Magium\Configuration\Manager\Manager;
10
use Zend\Cache\StorageFactory;
11
12
class MagiumConfigurationFactory
13
{
14
    protected $file;
15
    protected $xml;
16
17
    protected $manager;
18
    protected $builder;
19
20
    public function __construct($magiumConfigurationFile = null)
21
    {
22
        if (!$magiumConfigurationFile) {
23
            $cwd = __DIR__;
24
            $baseDir = realpath(DIRECTORY_SEPARATOR);
25
            while ($cwd && $cwd != $baseDir && file_exists($cwd)) {
26
                $checkFile = $cwd . DIRECTORY_SEPARATOR . 'magium-configuration.xml';
27
                if (file_exists($checkFile)) {
28
                    $magiumConfigurationFile = $checkFile;
29
                    break;
30
                }
31
                $lastPos = strrpos($cwd, DIRECTORY_SEPARATOR);
32
                $cwd = substr($cwd, 0, $lastPos);
33
            }
34
        }
35
36
        if (file_exists($magiumConfigurationFile)) {
37
            $this->file = realpath($magiumConfigurationFile);
38
        } else {
39
            throw new InvalidConfigurationFileException('Unable to file configuration file: ' . $magiumConfigurationFile);
40
        }
41
        chdir(dirname($this->file));
42
        $this->xml = simplexml_load_file($magiumConfigurationFile);
43
    }
44
45
    public function validateConfigurationFile()
46
    {
47
        $result = false;
48
        try {
49
            $doc = new \DOMDocument();
50
            $doc->load($this->file);
51
            $result = $doc->schemaValidate(__DIR__ . '/../assets/magium-configuration.xsd');
52
        } catch (\Exception $e) {
53
            // $result value is already set
54
        }
55
        return $result;
56
    }
57
58
    /**
59
     * Retrieves an instance of the cache based off of the XML cache configuration
60
     *
61
     * @param \SimpleXMLElement $element
62
     * @return \Zend\Cache\Storage\StorageInterface
63
     */
64
65
    protected function getCache(\SimpleXMLElement $element)
66
    {
67
        $cacheFactory = new CacheFactory();
68
        return $cacheFactory->getCache($element);
69
    }
70
71
    public function getBuilder()
72
    {
73
        if (!$this->builder instanceof Builder) {
74
            $builderFactoryConfig = $this->xml->builderFactory;
1 ignored issue
show
Bug introduced by
The property builderFactory does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
75
            $class = (string)$builderFactoryConfig['class'];
76
            if (!$class) {
77
                $class = BuilderFactory::class; // das default
78
            }
79
            $reflection = new \ReflectionClass($class);
80
            if (!$reflection->implementsInterface(BuilderFactoryInterface::class)) {
81
                throw new InvalidConfigurationException($class . ' must implement ' . BuilderFactoryInterface::class);
82
            }
83
            $builderFactory = $reflection->newInstance($this->xml);
84
            if (!$builderFactory instanceof BuilderFactoryInterface) {
85
                throw new InvalidConfigurationException(
86
                    sprintf(
87
                        'The builder factory %s must implement %s',
88
                        get_class($builderFactory),
89
                        BuilderFactoryInterface::class
90
                    )
91
                );
92
            }
93
            $this->builder = $builderFactory->getBuilder();
94
        }
95
        return $this->builder;
96
    }
97
98
    public function getManager()
99
    {
100
        if (!$this->manager instanceof Manager) {
101
            $cacheConfig = $this->xml->cache;
1 ignored issue
show
Bug introduced by
The property cache does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
102
            $globalAdapter = $this->getCache($cacheConfig);
103
            $localCache = null;
104
            $localCacheConfig = $this->xml->localCache;
1 ignored issue
show
Bug introduced by
The property localCache does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
105
            if ($localCacheConfig) {
106
                $localCache = $this->getCache($localCacheConfig);
107
            }
108
            $this->manager = new Manager($globalAdapter, $this->getBuilder(), $localCache);
109
        }
110
        return $this->manager;
111
    }
112
113
}
114