Passed
Push — develop ( a6220b...8d9e7b )
by Kevin
06:18 queued 02:39
created

BuilderFactory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 79.63%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 0
loc 110
ccs 43
cts 54
cp 0.7963
rs 10
c 0
b 0
f 0

7 Methods

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