Completed
Pull Request — master (#79)
by Jan Philipp
01:36
created

YamlConfigFileLoader::fixPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 3
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 extends ConfigFileLoader
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
     * @var string
37
     */
38
    private $applicationRootDirectory;
39
40
    /**
41
     * @param Parser $yamlReader
42
     * @param ConfigBuilder $configBuilder
43
     * @param string $applicationRootDirectory
44
     */
45
    public function __construct(Parser $yamlReader, ConfigBuilder $configBuilder, string $applicationRootDirectory)
46
    {
47
        $this->yamlReader = $yamlReader;
48
        $this->configBuilder = $configBuilder;
49
        $this->applicationRootDirectory = $applicationRootDirectory;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function isSupported(string $file): bool
56
    {
57
        return in_array(pathinfo($file, PATHINFO_BASENAME), ['.psh.yaml', '.psh.yml', '.psh.yml.dist', '.psh.yml.override', '.psh.yaml.dist', '.psh.yaml.override'], true);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 View Code Duplication
    public function load(string $file, array $params): Config
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
64
    {
65
        $contents = $this->loadFileContents($file);
66
        $rawConfigData = $this->parseFileContents($contents);
67
68
        $this->configBuilder->start();
69
70
        $this->configBuilder
71
            ->setHeader(
72
                $this->extractData(self::KEY_HEADER, $rawConfigData, '')
73
            );
74
75
        $this->setConfigData($file, $rawConfigData);
76
77
        $environments = $this->extractData(self::KEY_ENVIRONMENTS, $rawConfigData, []);
78
79
        foreach ($environments as $name => $data) {
80
            $this->configBuilder->start($name);
81
            $this->setConfigData($file, $data);
82
        }
83
84
        return $this->configBuilder
85
            ->create($params);
86
    }
87
88
    /**
89
     * @param string $file
90
     * @param array $rawConfigData
91
     */
92
    private function setConfigData(string $file, array $rawConfigData)
93
    {
94
        $this->configBuilder->setCommandPaths(
95
            $this->extractCommandPaths($file, $rawConfigData)
96
        );
97
98
        $this->configBuilder->setDynamicVariables(
99
            $this->extractData(self::KEY_DYNAMIC_VARIABLES, $rawConfigData, [])
100
        );
101
102
        $this->configBuilder->setConstants(
103
            $this->extractData(self::KEY_CONST_VARIABLES, $rawConfigData, [])
104
        );
105
106
        $this->configBuilder->setTemplates(
107
            $this->extractTemplates($file, $rawConfigData)
108
        );
109
    }
110
111
    /**
112
     * @param string $key
113
     * @param array $rawConfig
114
     * @param mixed $default
115
     * @return mixed|null
116
     */
117
    private function extractData(string $key, array $rawConfig, $default = false)
118
    {
119
        if (!array_key_exists($key, $rawConfig)) {
120
            return $default;
121
        }
122
123
        return $rawConfig[$key];
124
    }
125
126
127
128
    /**
129
     * @param string $contents
130
     * @return array
131
     */
132
    private function parseFileContents(string $contents): array
133
    {
134
        return $this->yamlReader->parse($contents);
135
    }
136
137
    /**
138
     * @param string $file
139
     * @param $rawConfigData
140
     * @return array
141
     */
142
    private function extractCommandPaths(string $file, array $rawConfigData): array
143
    {
144
        $paths = $this->extractData(self::KEY_COMMAND_PATHS, $rawConfigData, []);
145
146
        return array_map(function ($path) use ($file) {
147
            return $this->fixPath($this->applicationRootDirectory, $path, $file);
148
        }, $paths);
149
    }
150
151
    /**
152
     * @param string $file
153
     * @param array $rawConfigData
154
     * @return array
155
     */
156
    private function extractTemplates(string $file, array $rawConfigData): array
157
    {
158
        $templates = $this->extractData(self::KEY_TEMPLATES, $rawConfigData, []);
159
160
        return array_map(function ($template) use ($file) {
161
            $template['source'] = $this->fixPath($this->applicationRootDirectory, $template['source'], $file);
162
            $template['destination'] = $this->makeAbsolutePath($file, $template['destination']);
163
164
            return $template;
165
        }, $templates);
166
    }
167
}
168