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

BuilderFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Magium\Configuration\Config;
4
5
use Magium\Configuration\Config\Storage\RelationalDatabase;
6
use Magium\Configuration\File\Configuration\ConfigurationFileRepository;
7
use Magium\Configuration\Manager\CacheFactory;
8
use Zend\Db\Adapter\Adapter;
9
10
class BuilderFactory implements BuilderFactoryInterface
11
{
12
    protected $configuration;
13
    protected $adapter;
14 2
15
    public function __construct(\SimpleXMLElement $configuration)
16 2
    {
17 2
        $this->configuration = $configuration;
18
    }
19 2
20
    protected function getCache(\SimpleXMLElement $element)
21 2
    {
22 2
        $cacheFactory = new CacheFactory();
23
        return $cacheFactory->getCache($element);
24
    }
25 2
26
    public function getAdapter()
27 2
    {
28 2
        if (!$this->adapter instanceof Adapter) {
29 2
            $config = json_encode($this->configuration->persistenceConfiguration);
30 2
            $config = json_decode($config, true);
31 2
            $this->adapter = new Adapter($config);
32
        }
33
        return $this->adapter;
34 2
    }
35
36 2
    public function getPersistence()
37 2
    {
38 2
        $persistence = new RelationalDatabase($this->getAdapter());
39 2
        return $persistence;
40
    }
41 2
42 1
    protected function getSecureBaseDirectories()
43 1
    {
44 1
        $config = json_encode($this->configuration->configurationDirectories);
45
        $config = json_decode($config, true);
46
        $baseDirs = [];
47
        if (is_array($config)) {
48
            // This code depends on chdir() having been called in MagiumConfigurationFactory
49 1
            foreach ($config as $dir) {
50
                $path = realpath($dir);
51
                if (!is_dir($path)) {
52 2
                    throw new InvalidConfigurationLocationException('A secure configuration path cannot be determined for the directory: ' . $dir);
53
                }
54
                $baseDirs[] = $path;
55 2
            }
56 2
        }
57 2
        return $baseDirs;
58 1
    }
59
60
    protected function getConfigurationFiles(array $secureBaseDirectories = [])
61
    {
62
        $config = json_encode($this->configuration->configurationFiles);
63
        $config = json_decode($config, true);
64
        $files = [];
65
        foreach ($config as $file) {
66
            $found = false;
67 1
            foreach ($secureBaseDirectories as $base) {
68
                chdir($base);
69
                $path = realpath($file);
70
                if ($path) {
71
                    $found = true;
72
                    $files[] = $path;
73 1
                }
74
            }
75
            if (!$found) {
76
                throw new InvalidConfigurationLocationException('Could not find file: ' . $file);
77
            }
78
        }
79
        return $files;
80
    }
81
82
    public function getBuilder()
83
    {
84
        // This method expects that chdir() has been called on the same level as the magium-configuration.xml file
85
        $cache = $this->getCache($this->configuration->cache);
86
        $persistence = $this->getPersistence();
87
        $secureBases = $this->getSecureBaseDirectories();
88
        $configurationFiles = $this->getConfigurationFiles($secureBases);
89
        $repository = new ConfigurationFileRepository($secureBases, $configurationFiles);
90
91
        /*
92
         * We only populate up to the secureBases because adding a DIC or service manager by configuration starts
93
         * making the configuration-based approach pay off less.  If you need a DIC or service manager for your
94
         * configuration builder (which you will if you use object/method callbacks for value filters) then you need
95
         * to wire the Builder object with your own code.
96
         */
97
98
        $builder = new Builder(
99
            $cache,
100
            $persistence,
101
            $repository
102
        );
103
104
        return $builder;
105
    }
106
107
}
108