Passed
Pull Request — develop (#18)
by Kevin
03:58 queued 41s
created

MagiumConfigurationFactory::buildContextFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 4
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\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
    public function __construct($magiumConfigurationFile = null)
25
    {
26
        if (!$magiumConfigurationFile) {
27
            $cwd = __DIR__;
28
            $baseDir = realpath(DIRECTORY_SEPARATOR);
29
            while ($cwd && $cwd != $baseDir && file_exists($cwd)) {
30
                $checkFile = $cwd . DIRECTORY_SEPARATOR . 'magium-configuration.xml';
31
                if (file_exists($checkFile)) {
32
                    $magiumConfigurationFile = $checkFile;
33
                    break;
34
                }
35
                $lastPos = strrpos($cwd, DIRECTORY_SEPARATOR);
36
                $cwd = substr($cwd, 0, $lastPos);
37
            }
38
        }
39
40
        if (file_exists($magiumConfigurationFile)) {
41
            $this->file = realpath($magiumConfigurationFile);
42
        } else {
43
            throw new InvalidConfigurationFileException('Unable to file configuration file: ' . $magiumConfigurationFile);
44
        }
45
        $this->baseDir = dirname($this->file);
46
        chdir($this->baseDir);
47
        $this->xml = simplexml_load_file($magiumConfigurationFile);
48
    }
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
    }
68
69
    public function getContextFile()
70
    {
71
        if (!$this->contextFile) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
72
//            if ($this->)
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
        }
74
        return $this->contextFile;
75
    }
76
77
    public function validateConfigurationFile()
78
    {
79
        $result = false;
80
        try {
81
            $doc = new \DOMDocument();
82
            $doc->load($this->file);
83
            $result = $doc->schemaValidate(__DIR__ . '/../assets/magium-configuration.xsd');
84
        } catch (\Exception $e) {
85
            // $result value is already set
86
        }
87
        return $result;
88
    }
89
90
    /**
91
     * Retrieves an instance of the cache based off of the XML cache configuration
92
     *
93
     * @param \SimpleXMLElement $element
94
     * @return \Zend\Cache\Storage\StorageInterface
95
     */
96
97
    protected function getCache(\SimpleXMLElement $element)
98
    {
99
        $cacheFactory = new CacheFactory();
100
        return $cacheFactory->getCache($element);
101
    }
102
103
    /**
104
     * @return Builder
105
     */
106
    public function getBuilder()
107
    {
108
        if (!$this->builder instanceof Builder) {
109
            $builderFactoryConfig = $this->xml->builderFactory;
110
            $class = (string)$builderFactoryConfig['class'];
111
            if (!$class) {
112
                $class = BuilderFactory::class; // das default
113
            }
114
            $reflection = new \ReflectionClass($class);
115
            if (!$reflection->implementsInterface(BuilderFactoryInterface::class)) {
116
                throw new InvalidConfigurationException($class . ' must implement ' . BuilderFactoryInterface::class);
117
            }
118
            $builderFactory = $reflection->newInstance($this->xml);
119
            if (!$builderFactory instanceof BuilderFactoryInterface) {
120
                throw new InvalidConfigurationException(
121
                    sprintf(
122
                        'The builder factory %s must implement %s',
123
                        get_class($builderFactory),
124
                        BuilderFactoryInterface::class
125
                    )
126
                );
127
            }
128
            $this->builder = $builderFactory->getBuilder();
129
        }
130
        return $this->builder;
131
    }
132
133
    protected function getRemoteCache()
134
    {
135
        $cacheConfig = $this->xml->cache;
136
        $globalAdapter = $this->getCache($cacheConfig);
137
        return $globalAdapter;
138
    }
139
140
    protected function getLocalCache()
141
    {
142
        $localCache = null;
143
        $localCacheConfig = $this->xml->localCache;
144
        if ($localCacheConfig) {
145
            $localCache = $this->getCache($localCacheConfig);
146
        }
147
        return $localCache;
148
    }
149
150
    public function getManager()
151
    {
152
        if (!$this->manager instanceof Manager) {
153
            $this->manager = new Manager($this->getRemoteCache(), $this->getBuilder(), $this->getLocalCache());
154
        }
155
        return $this->manager;
156
    }
157
158
}
159