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

BuilderFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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