Test Failed
Pull Request — develop (#19)
by Kevin
02:51
created

MagiumConfigurationFactory   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 71.05%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 30
lcom 2
cbo 7
dl 0
loc 164
ccs 54
cts 76
cp 0.7105
rs 10
c 2
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getRemoteCache() 0 6 1
A getLocalCache() 0 9 2
C __construct() 0 25 7
A buildContextFile() 0 19 4
A getContextFile() 0 7 2
A validateConfigurationFile() 0 12 2
A getCache() 0 5 1
A getBuilderFactory() 0 16 4
A getBuilder() 0 7 2
B getManager() 0 25 5
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 9
25
    public function __construct($magiumConfigurationFile = null)
26 9
    {
27 4
        if (!$magiumConfigurationFile) {
28 4
            $cwd = __DIR__;
29 4
            $baseDir = realpath(DIRECTORY_SEPARATOR);
30 4
            while ($cwd && $cwd != $baseDir && file_exists($cwd)) {
31 4
                $checkFile = $cwd . DIRECTORY_SEPARATOR . 'magium-configuration.xml';
32 3
                if (file_exists($checkFile)) {
33 3
                    $magiumConfigurationFile = $checkFile;
34
                    break;
35 4
                }
36 4
                $lastPos = strrpos($cwd, DIRECTORY_SEPARATOR);
37
                $cwd = substr($cwd, 0, $lastPos);
38
            }
39
        }
40 9
41 8
        if (file_exists($magiumConfigurationFile)) {
42
            $this->file = realpath($magiumConfigurationFile);
43 1
        } else {
44
            throw new InvalidConfigurationFileException('Unable to file configuration file: ' . $magiumConfigurationFile);
45 8
        }
46 8
        $this->baseDir = dirname($this->file);
47 8
        chdir($this->baseDir);
48 8
        $this->xml = simplexml_load_file($magiumConfigurationFile);
49
    }
50
51
    protected function buildContextFile()
52
    {
53
        chdir($this->baseDir);
54
        $contextFileCheck = (string)$this->xml->contextConfigurationFile['file'];
55
        $contextFileType = (string)$this->xml->contextConfigurationFile['type'];
56
        $contextFile = realpath($contextFileCheck);
57
        if (!$contextFile) {
58
            throw new MissingConfigurationException('Unable to find context file: ' . $contextFileCheck);
59
        }
60
        $class = 'Magium\Configuration\File\Context\\' . ucfirst($contextFileType) . 'File';
61
        $reflectionClass = new \ReflectionClass($class);
62
        if ($reflectionClass->isSubclassOf(AbstractContextConfigurationFile::class)) {
63
            $instance = $reflectionClass->newInstance($contextFile);
64
            if ($instance instanceof AbstractContextConfigurationFile) {
65
                return $instance;
66
            }
67
        }
68
        throw new InvalidConfigurationException('Unable to load context configuration file: ' . $contextFileCheck);
69
    }
70
71
    public function getContextFile()
72
    {
73
        if (!$this->contextFile instanceof AbstractContextConfigurationFile) {
74
            $this->contextFile = $this->buildContextFile();
75
        }
76
        return $this->contextFile;
77
    }
78 2
79
    public function validateConfigurationFile()
80 2
    {
81
        $result = false;
82 2
        try {
83 2
            $doc = new \DOMDocument();
84 2
            $doc->load($this->file);
85 1
            $result = $doc->schemaValidate(__DIR__ . '/../assets/magium-configuration.xsd');
86
        } catch (\Exception $e) {
87
            // $result value is already set
88 2
        }
89
        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 2
99
    protected function getCache(\SimpleXMLElement $element)
100 2
    {
101 2
        $cacheFactory = new CacheFactory();
102
        return $cacheFactory->getCache($element);
103
    }
104
105
    public function getBuilderFactory()
106
    {
107 2
        if (!$this->builderFactory instanceof BuilderFactoryInterface) {
108
            $builderFactoryConfig = $this->xml->builderFactory;
109 2
            $class = (string)$builderFactoryConfig['class'];
110 2
            if (!$class) {
111 2
                $class = BuilderFactory::class; // das default
112 2
            }
113 2
            $reflection = new \ReflectionClass($class);
114
            if (!$reflection->implementsInterface(BuilderFactoryInterface::class)) {
115 2
                throw new InvalidConfigurationException($class . ' must implement ' . BuilderFactoryInterface::class);
116 2
            }
117
            $this->builderFactory = $reflection->newInstance($this->xml);
118
        }
119 2
        return $this->builderFactory;
120 2
    }
121
122
    /**
123
     * @return BuilderInterface
124
     */
125
126
    public function getBuilder()
127
    {
128
        if (!$this->builder instanceof BuilderInterface) {
129 2
            $this->builder = $this->getBuilderFactory()->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 ManagerInterface) {
154 4
            $managerClass = Manager::class;
155
            if (isset($this->xml->manager['class'])) {
156 3
                $managerClass = $this->xml->manager['class'];
157
            }
158
            $reflectionClass = new \ReflectionClass($managerClass);
159
            if ($managerClass == Manager::class) {
160
                // just a shortcut so I don't have to rewrite some complicated unit tests.  I'm just lazy.
161
                $this->manager = new Manager($this->getLocalCache(), $this->getBuilder(), $this->getRemoteCache());
162
                return $this->manager;
163
            }
164
            if (!$reflectionClass->implementsInterface(ManagerInterface::class)) {
165
                throw new InvalidConfigurationException('Manager class must implement ' . ManagerInterface::class);
166
            }
167
            $manager = $reflectionClass->newInstance();
168
            /* @var $manager ManagerInterface */
169
            $manager->setBuilder($this->getBuilder());
170
            $manager->setLocalCache($this->getLocalCache());
0 ignored issues
show
Bug introduced by
It seems like $this->getLocalCache() can be null; however, setLocalCache() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
171
            $manager->setRemoteCache($this->getRemoteCache());
172
            $this->manager = $manager;
173
        }
174
        return $this->manager;
175
    }
176
177
}
178