Completed
Pull Request — master (#10)
by Tomáš
06:04 queued 03:12
created

YamlFileConfigurationBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 62.96%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 65
ccs 17
cts 27
cp 0.6296
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C internalBuild() 0 49 10
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\Configuration;
13
14
use Dflydev\DotAccessConfiguration\AbstractConfigurationBuilder;
15
use Dflydev\DotAccessConfiguration\ConfigurationInterface;
16
use Dflydev\DotAccessData\Util as DotAccessDataUtil;
17
use Symfony\Component\Yaml\Yaml;
18
19
final class YamlFileConfigurationBuilder extends AbstractConfigurationBuilder
20
{
21
    /**
22
     * @var string[]
23
     */
24
    private $yamlConfigurationFilenames;
25
26 1
    public function __construct(array $yamlConfigurationFilenames)
27
    {
28 1
        $this->yamlConfigurationFilenames = $yamlConfigurationFilenames;
29 1
    }
30
31
    /**
32
     * {@inheritdoc}.
33
     */
34 1
    public function internalBuild(ConfigurationInterface $configuration)
35
    {
36 1
        $config = [];
37 1
        $imports = [];
38 1
        foreach ($this->yamlConfigurationFilenames as $yamlConfigurationFilename) {
39 1
            if (file_exists($yamlConfigurationFilename)) {
40 1
                $config = DotAccessDataUtil::mergeAssocArray(
41 1
                    $config, Yaml::parse(file_get_contents($yamlConfigurationFilename))
42
                );
43
44 1
                if (isset($config['imports'])) {
45
                    foreach ((array) $config['imports'] as $file) {
46
                        if (0 === strpos($file, '/')) {
47
                            // Absolute path
48
                            $imports[] = $file;
49
                        } else {
50
                            if ($realpath = realpath(dirname($yamlConfigurationFilename).'/'.$file)) {
51 1
                                $imports[] = $realpath;
52
                            }
53
                        }
54
                    }
55
                }
56
            }
57
        }
58
59 1
        if ($imports) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $imports of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
60
            $importsBuilder = new static($imports);
61
62
            // We want to reconfigure the imports builder to have the
63
            // same basic configuration as this instance.
64
            $this->reconfigure($importsBuilder);
65
66
            $configuration->import($importsBuilder->build());
67
68
            $internalImports = $configuration->get('imports');
69
        } else {
70 1
            $internalImports = null;
71
        }
72
73 1
        $configuration->importRaw($config);
74
75 1
        if ($internalImports) {
76
            foreach ((array) $internalImports as $import) {
77
                $configuration->append('imports', $import);
78
            }
79
        }
80
81 1
        return $configuration;
82
    }
83
}
84