Completed
Push — master ( 937114...74cc4f )
by Thomas
14s
created

YamlConfigFileLoader::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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