Completed
Push — master ( 63a11a...de1fa2 )
by Jan Philipp
12s
created

YamlConfigFileLoader::extractCommandPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 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
        return in_array(pathinfo($file, PATHINFO_EXTENSION), ['yaml', 'yml', 'dist', 'override'], true);
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function load(string $file, array $params): Config
57
    {
58
        $contents = $this->loadFileContents($file);
59
        $rawConfigData = $this->parseFileContents($contents);
60
61
        $this->configBuilder->start();
62
63
        $this->configBuilder
64
            ->setHeader(
65
                $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...
66
            );
67
68
        $this->setConfigData($file, $rawConfigData);
69
70
        $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...
71
72
        foreach ($environments as $name => $data) {
73
            $this->configBuilder->start($name);
74
            $this->setConfigData($file, $data);
75
        }
76
77
        return $this->configBuilder
78
            ->create($params);
79
    }
80
81
    /**
82
     * @param string $file
83
     * @param array $rawConfigData
84
     */
85
    private function setConfigData(string $file, array $rawConfigData)
86
    {
87
        $this->configBuilder->setCommandPaths(
88
            $this->extractCommandPaths($file, $rawConfigData)
89
        );
90
91
        $this->configBuilder->setDynamicVariables(
92
            $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...
93
        );
94
95
        $this->configBuilder->setConstants(
96
            $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...
97
        );
98
99
        $this->configBuilder->setTemplates(
100
            array_map(function ($template) use ($file) {
101
                $template['source'] = $this->fixPath($template['source'], $file);
102
                $template['destination'] = $this->fixPath($template['destination'], $file);
103
104
                return $template;
105
            }, $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...
106
        );
107
    }
108
109
    /**
110
     * @param string $key
111
     * @param array $rawConfig
112
     * @param bool $default
113
     * @return mixed|null
114
     */
115
    private function extractData(string $key, array $rawConfig, $default = false)
116
    {
117
        if (!array_key_exists($key, $rawConfig)) {
118
            return $default;
119
        }
120
121
        return $rawConfig[$key];
122
    }
123
124
    /**
125
     * @param string $file
126
     * @return string
127
     */
128
    private function loadFileContents(string $file): string
129
    {
130
        return file_get_contents($file);
131
    }
132
133
    /**
134
     * @param string $contents
135
     * @return array
136
     */
137
    private function parseFileContents(string $contents): array
138
    {
139
        return $this->yamlReader->parse($contents);
140
    }
141
142
    /**
143
     * @param string $file
144
     * @param $rawConfigData
145
     * @return array
146
     */
147
    private function extractCommandPaths(string $file, array $rawConfigData): array
148
    {
149
        $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...
150
151
        return array_map(function ($path) use ($file) {
152
            return $this->fixPath($path, $file);
153
        }, $paths);
154
    }
155
156
    /**
157
     * @param string $absoluteOrRelativePath
158
     * @param string $baseFile
159
     * @return string
160
     */
161
    private function fixPath(string $absoluteOrRelativePath, string $baseFile): string
162
    {
163
        if (file_exists($absoluteOrRelativePath)) {
164
            return $absoluteOrRelativePath;
165
        }
166
167
        return pathinfo($baseFile, PATHINFO_DIRNAME) . '/' . $absoluteOrRelativePath;
168
    }
169
}
170