Completed
Pull Request — master (#82)
by Jan Philipp
02:07
created

ProcessEnvironment   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 100
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A initializeConstants() 0 9 2
A initializeVariables() 0 10 2
A initializeTemplates() 0 9 2
A getAllValues() 0 7 1
A getTemplates() 0 4 1
A createProcess() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\ScriptRuntime\Execution;
4
5
use Symfony\Component\Process\Process;
6
7
/**
8
 * Create representation of the current environment variables and constants
9
 */
10
class ProcessEnvironment
11
{
12
    /**
13
     * @var ValueProvider[]
14
     */
15
    private $constants;
16
17
    /**
18
     * @var ValueProvider[]
19
     */
20
    private $variables;
21
22
    /**
23
     * @var Template[]
24
     */
25
    private $templates;
26
27
    /**
28
     * @param array $constants
29
     * @param array $variables
30
     * @param array $templates
31
     */
32
    public function __construct(array $constants, array $variables, array $templates)
33
    {
34
        $this->constants = $this->initializeConstants($constants);
35
        $this->variables = $this->initializeVariables($variables);
36
        $this->templates = $this->initializeTemplates($templates);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->initializeTemplates($templates) of type array<integer,object<Sho...ecution\ValueProvider>> is incompatible with the declared type array<integer,object<Sho...me\Execution\Template>> of property $templates.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
    }
38
39
    /**
40
     * @param array $constants
41
     * @return ValueProvider[]
42
     */
43
    private function initializeConstants(array $constants): array
44
    {
45
        $resolvedValues = [];
46
        foreach ($constants as $name => $value) {
47
            $resolvedValues[$name] = new SimpleValueProvider((string) $value);
48
        }
49
50
        return $resolvedValues;
51
    }
52
53
    /**
54
     * @param array $variables
55
     * @return ValueProvider[]
56
     */
57
    private function initializeVariables(array $variables): array
58
    {
59
        $resolvedVariables = [];
60
        foreach ($variables as $name => $shellCommand) {
61
            $process = $this->createProcess($shellCommand);
62
            $resolvedVariables[$name] = new ProcessValueProvider($process);
63
        }
64
65
        return $resolvedVariables;
66
    }
67
68
    /**
69
     * @param array $templates
70
     * @return ValueProvider[]
71
     */
72
    private function initializeTemplates(array $templates): array
73
    {
74
        $resolvedVariables = [];
75
        foreach ($templates as $template) {
76
            $resolvedVariables[] = new Template($template['source'], $template['destination']);
77
        }
78
79
        return $resolvedVariables;
80
    }
81
82
    /**
83
     * @return ValueProvider[]
84
     */
85
    public function getAllValues(): array
86
    {
87
        return array_merge(
88
            $this->constants,
89
            $this->variables
90
        );
91
    }
92
93
    /**
94
     * @return Template[]
95
     */
96
    public function getTemplates(): array
97
    {
98
        return $this->templates;
99
    }
100
101
    /**
102
     * @param string $shellCommand
103
     * @return Process
104
     */
105
    public function createProcess(string $shellCommand): Process
106
    {
107
        return new Process($shellCommand);
108
    }
109
}
110