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

ApplicationFactory::createScriptFinder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
4
namespace Shopware\Psh\Application;
5
6
use Shopware\Psh\Config\Config;
7
use Shopware\Psh\Config\ConfigBuilder;
8
use Shopware\Psh\Config\ConfigFileFinder;
9
use Shopware\Psh\Config\ConfigMerger;
10
use Shopware\Psh\Config\YamlConfigFileLoader;
11
use Shopware\Psh\Listing\DescriptionReader;
12
use Shopware\Psh\Listing\Script;
13
use Shopware\Psh\Listing\ScriptFinder;
14
use Shopware\Psh\ScriptRuntime\ProcessCommand;
15
use Shopware\Psh\ScriptRuntime\CommandBuilder;
16
use Shopware\Psh\ScriptRuntime\Logger;
17
use Shopware\Psh\ScriptRuntime\ProcessEnvironment;
18
use Shopware\Psh\ScriptRuntime\ProcessExecutor;
19
use Shopware\Psh\ScriptRuntime\ScriptLoader;
20
use Shopware\Psh\ScriptRuntime\TemplateEngine;
21
use Symfony\Component\Yaml\Parser;
22
23
/**
24
 * Create the various interdependent objects for the application.
25
 */
26
class ApplicationFactory
27
{
28
    /**
29
     * @param string $rootDirectory
30
     * @return Config
31
     * @throws \RuntimeException
32
     */
33
    public function createConfig(string $rootDirectory, array $params): Config
34
    {
35
        $configFinder = new ConfigFileFinder();
36
        $configFiles = $configFinder->discoverFiles($rootDirectory);
37
38
        $configLoader = new YamlConfigFileLoader(new Parser(), new ConfigBuilder());
39
40
        $configs = [];
41
        foreach ($configFiles as $configFile) {
42
            if (!$configLoader->isSupported($configFile)) {
43
                throw new \RuntimeException('Unable to read configuration from "' . $configFile . '"');
44
            }
45
46
            $configs[] = $configLoader->load($configFile, $this->reformatParams($params));
47
        }
48
49
        $merger = new ConfigMerger();
50
        
51
        return $merger->merge(...$configs);
0 ignored issues
show
Documentation introduced by
$configs is of type array, but the function expects a object<Shopware\Psh\Config\Config>.

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...
52
    }
53
54
    /**
55
     * @param Config $config
56
     * @return ScriptFinder
57
     */
58
    public function createScriptFinder(Config $config): ScriptFinder
59
    {
60
        return new ScriptFinder($config->getAllScriptPaths(), new DescriptionReader());
61
    }
62
63
    /**
64
     * @param Script $script
65
     * @param Config $config
66
     * @param Logger $logger
67
     * @param string $rootDirectory
68
     * @return ProcessExecutor
69
     */
70
    public function createProcessExecutor(
71
        Script $script,
72
        Config $config,
73
        Logger $logger,
74
        string $rootDirectory
75
    ): ProcessExecutor {
76
        return new ProcessExecutor(
77
            new ProcessEnvironment(
78
                $config->getConstants($script->getEnvironment()),
79
                $config->getDynamicVariables($script->getEnvironment()),
80
                $config->getTemplates($script->getEnvironment())
81
            ),
82
            new TemplateEngine(),
83
            $logger,
84
            $rootDirectory
85
        );
86
    }
87
88
    /**
89
     * @param Script $script
90
     * @return ProcessCommand[]
91
     */
92
    public function createCommands(Script $script): array
93
    {
94
        $scriptLoader = new ScriptLoader(new CommandBuilder());
95
        return $scriptLoader->loadScript($script);
96
    }
97
98
    /**
99
     * @param array $params
100
     * @return array
101
     */
102
    private function reformatParams(array $params): array
103
    {
104
        if (count($params) < 2) {
105
            return [];
106
        }
107
108
        $reformattedParams = [];
109
        for ($i = 2; $i < count($params); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
110
            $key = $params[$i];
111
112
            if (strpos($key, '--') !== 0) {
113
                throw new InvalidParameterException(
114
                    sprintf('Unable to parse parameter %s. Use -- for correct usage', $key)
115
                );
116
            }
117
118
            if (strpos($key, '=')) {
119
                list($key, $value) = explode('=', $key, 2);
120
121
                if (strpos($value, '"') === 0) {
122
                    $value = substr($value, 1, -1);
123
                }
124
            } else {
125
                $i++;
126
                $value = $params[$i];
127
            }
128
129
            $key = str_replace('--', '', $key);
130
            $reformattedParams[strtoupper($key)] = $value;
131
        }
132
133
        return $reformattedParams;
134
    }
135
}
136