Completed
Pull Request — master (#42)
by Simon
01:46
created

YamlConfigFileLoader   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 174
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isSupported() 0 5 2
A removeDistExtension() 0 9 2
B load() 0 24 2
A setConfigData() 0 23 1
A extractData() 0 8 2
A loadFileContents() 0 4 1
A parseFileContents() 0 4 1
A extractCommandPaths() 0 8 1
A fixPath() 0 8 2
1
<?php declare(strict_types=1);
2
3
4
namespace Shopware\Psh\Config;
5
6
use Symfony\Component\Yaml\Parser;
7
8
/**
9
 * Load the config data from a yaml file
10
 */
11
class YamlConfigFileLoader implements ConfigLoader
12
{
13
    const KEY_HEADER = 'header';
14
15
    const KEY_DYNAMIC_VARIABLES = 'dynamic';
16
17
    const KEY_CONST_VARIABLES = 'const';
18
19
    const KEY_COMMAND_PATHS = 'paths';
20
21
    const KEY_ENVIRONMENTS = 'environments';
22
23
    const KEY_TEMPLATES = 'templates';
24
25
    /**
26
     * @var Parser
27
     */
28
    private $yamlReader;
29
30
    /**
31
     * @var ConfigBuilder
32
     */
33
    private $configBuilder;
34
35
    /**
36
     * @param Parser $yamlReader
37
     * @param ConfigBuilder $configBuilder
38
     */
39
    public function __construct(Parser $yamlReader, ConfigBuilder $configBuilder)
40
    {
41
        $this->yamlReader = $yamlReader;
42
        $this->configBuilder = $configBuilder;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function isSupported(string $file): bool
49
    {
50
        $file = $this->removeDistExtension($file);
51
        return pathinfo($file, PATHINFO_EXTENSION) === 'yaml' || pathinfo($file, PATHINFO_EXTENSION) === 'yml';
52
    }
53
54
    /**
55
     * @param string $file
56
     * @return string
57
     */
58
    private function removeDistExtension(string $file): string
59
    {
60
        $fileInfo = pathinfo($file);
61
        if ($fileInfo['extension'] === 'dist') {
62
            $file = $fileInfo['filename'];
63
        }
64
65
        return $file;
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function load(array $files): Config
72
    {
73
        $file = $files[0];
74
        $contents = $this->loadFileContents($file);
75
        $rawConfigData = $this->parseFileContents($contents);
76
77
        $this->configBuilder->start();
78
79
        $this->configBuilder
80
            ->setHeader(
81
                $this->extractData(self::KEY_HEADER, $rawConfigData, '')
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
            );
83
84
        $this->setConfigData($file, $rawConfigData);
85
86
        $environments = $this->extractData(self::KEY_ENVIRONMENTS, $rawConfigData, []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
        foreach ($environments as $name => $data) {
88
            $this->configBuilder->start($name);
89
            $this->setConfigData($file, $data);
90
        }
91
92
        return $this->configBuilder
93
            ->create();
94
    }
95
96
    /**
97
     * @param string $file
98
     * @param array $rawConfigData
99
     */
100
    private function setConfigData(string $file, array $rawConfigData)
101
    {
102
        $this->configBuilder->setCommandPaths(
103
            $this->extractCommandPaths($file, $rawConfigData)
104
        );
105
106
        $this->configBuilder->setDynamicVariables(
107
            $this->extractData(self::KEY_DYNAMIC_VARIABLES, $rawConfigData, [])
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
        );
109
110
        $this->configBuilder->setConstants(
111
            $this->extractData(self::KEY_CONST_VARIABLES, $rawConfigData, [])
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112
        );
113
114
        $this->configBuilder->setTemplates(
115
            array_map(function ($template) use ($file) {
116
                $template['source'] = $this->fixPath($template['source'], $file);
117
                $template['destination'] = $this->fixPath($template['destination'], $file);
118
119
                return $template;
120
            }, $this->extractData(self::KEY_TEMPLATES, $rawConfigData, []))
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
        );
122
    }
123
124
    /**
125
     * @param string $key
126
     * @param array $rawConfig
127
     * @param bool $default
128
     * @return mixed|null
129
     */
130
    private function extractData(string $key, array $rawConfig, $default = false)
131
    {
132
        if (!array_key_exists($key, $rawConfig)) {
133
            return $default;
134
        }
135
136
        return $rawConfig[$key];
137
    }
138
139
    /**
140
     * @param string $file
141
     * @return string
142
     */
143
    private function loadFileContents(string $file): string
144
    {
145
        return file_get_contents($file);
146
    }
147
148
    /**
149
     * @param string $contents
150
     * @return array
151
     */
152
    private function parseFileContents(string $contents): array
153
    {
154
        return $this->yamlReader->parse($contents);
155
    }
156
157
    /**
158
     * @param string $file
159
     * @param $rawConfigData
160
     * @return array
161
     */
162
    private function extractCommandPaths(string $file, array $rawConfigData): array
163
    {
164
        $paths = $this->extractData(self::KEY_COMMAND_PATHS, $rawConfigData, []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
165
166
        return array_map(function ($path) use ($file) {
167
            return $this->fixPath($path, $file);
168
        }, $paths);
169
    }
170
171
    /**
172
     * @param string $absoluteOrRelativePath
173
     * @param string $baseFile
174
     * @return string
175
     */
176
    private function fixPath(string $absoluteOrRelativePath, string $baseFile): string
177
    {
178
        if (file_exists($absoluteOrRelativePath)) {
179
            return $absoluteOrRelativePath;
180
        }
181
182
        return pathinfo($baseFile, PATHINFO_DIRNAME) . '/' . $absoluteOrRelativePath;
183
    }
184
}
185