Test Failed
Pull Request — develop (#36)
by Kevin
02:40
created

BuilderFactory   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 120
Duplicated Lines 5 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 79.63%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 7
dl 6
loc 120
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
B getSecureBaseDirectories() 3 23 6
C getConfigurationFiles() 3 26 7
B getBuilder() 0 24 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function getSecureBaseDirectories()
56
    {
57 3
        $cwd = getcwd();
58 3
        chdir($this->baseDirectory->getPath());
59 3
        $config = $this->configuration->configurationDirectories;
0 ignored issues
show
Bug introduced by
The property configurationDirectories does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
60 3
        $config = json_encode($config);
61 3
        $config = json_decode($config, true);
62
        $baseDirs = [];
63 3
        if (is_array($config) && isset($config['directory'])) {
64 1 View Code Duplication
            if (!is_array($config['directory'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65 1
                $config['directory'] = [$config['directory']];
66 1
            }
67
            foreach ($config['directory'] as $dir) {
68
                $path = realpath($dir);
69
                if (!is_dir($path)) {
70
                    throw new InvalidConfigurationLocationException('A secure configuration path cannot be determined for the directory: ' . $dir);
71 2
                }
72
                $baseDirs[] = $path;
73
            }
74 2
        }
75
        chdir($cwd);
76 2
        return $baseDirs;
77 2
    }
78 2
79 2
    public function getConfigurationFiles(array $secureBaseDirectories = [])
80
    {
81
        $config = json_encode($this->configuration->configurationFiles);
82
        $config = json_decode($config, true);
83
        $files = [];
84
        if (isset($config['file'])) {
85 View Code Duplication
            if (!is_array($config['file'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
                $config['file'] = [$config['file']];
87
            }
88
            foreach ($config['file'] as $file) {
89
                $found = false;
90
                foreach ($secureBaseDirectories as $base) {
91
                    chdir($base);
92
                    $path = realpath($file);
93 2
                    if ($path) {
94
                        $found = true;
95
                        $files[] = $path;
96 3
                    }
97
                }
98
                if (!$found) {
99 3
                    throw new InvalidConfigurationLocationException('Could not find file: ' . $file);
100 3
                }
101 3
            }
102 2
        }
103 2
        return $files;
104
    }
105
106
    public function getBuilder()
107
    {
108
        // This method expects that chdir() has been called on the same level as the magium-configuration.xml file
109
        $cache = $this->getCache($this->configuration->cache);
110
        $persistence = $this->getPersistence();
111
        $secureBases = $this->getSecureBaseDirectories();
112 2
        $configurationFiles = $this->getConfigurationFiles($secureBases);
113
        $repository = new ConfigurationFileRepository($secureBases, $configurationFiles);
114
115
        /*
116
         * We only populate up to the secureBases because adding a DIC or service manager by configuration starts
117
         * making the configuration-based approach pay off less.  If you need a DIC or service manager for your
118 2
         * configuration builder (which you will if you use object/method callbacks for value filters) then you need
119
         * to wire the Builder object with your own code.
120
         */
121
122
        $builder = new Builder(
123
            $cache,
124
            $persistence,
125
            $repository
126
        );
127
128
        return $builder;
129
    }
130
131
}
132