Completed
Branch test-shortgetter (f6e9fd)
by Alexander
01:20
created

Parser::checkByValue()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
rs 9.2222
c 0
b 0
f 0
cc 6
nc 5
nop 1
1
<?php
2
3
namespace CyberLine\SystemdState;
4
5
use CyberLine\SystemdState\Model\ExecCommand;
6
7
class Parser
8
{
9
    private static $arrayTypes = [
10
        'After',
11
        'Before',
12
        'Requires',
13
        'Conflicts',
14
        'WantedBy',
15
        'Names',
16
        'Wants',
17
        'RequiredBy',
18
        'RequiredByOverridable',
19
        'RequiresMountsFor',
20
        'ReadWritePaths',
21
        'ReadOnlyPaths',
22
        'DropInPaths',
23
        'ConflictedBy',
24
        'Documentation',
25
        'BindsTo',
26
        'Triggers',
27
        'Features',
28
        'UnitPath',
29
        'BoundBy',
30
    ];
31
32
    private static $execTypes = [
33
        'ExecReload',
34
        'ExecStartPre',
35
        'ExecStart',
36
        'ExecStartPost',
37
        'ExecStop',
38
        'ExecStopPost',
39
    ];
40
41
    /**
42
     * @param array $values
43
     * @return array|bool|ExecCommand|\DateTimeImmutable|int|mixed|null
44
     */
45
    public static function parseValueByContent(array $values)
46
    {
47
        if (!array_key_exists(1, $values)) {
48
            return null;
49
        }
50
51
        $key = $values[0];
52
53
        if (null !== ($value = self::checkByKey($key, $values))) {
0 ignored issues
show
introduced by
The condition null !== $value = self::checkByKey($key, $values) is always true.
Loading history...
54
            return $value;
55
        }
56
57
        if (null !== ($value = self::checkByValue($values[1]))) {
58
            return $value;
59
        }
60
61
        return $values[1];
62
    }
63
64
    /**
65
     * @param string $key
66
     * @param array $values
67
     * @return array|ExecCommand|\DateTimeImmutable|null
68
     */
69
    private static function checkByKey(string $key, array $values)
70
    {
71
        if ($key === 'Environment') {
72
            return self::parseEnvironmentLine($values);
73
        }
74
75
        if (in_array($key, self::$execTypes)) {
76
            array_shift($values);
77
            return self::parseExecLine(implode(' ', $values));
78
        }
79
80
        if (preg_match('/Timestamp$/', $key)) {
81
            return \date_create_immutable_from_format('* Y-m-d H:i:s e', $values[1]);
82
        }
83
84
        if (in_array($key, self::$arrayTypes)) {
85
            return explode(' ', $values[1]);
86
        }
87
88
        return null;
89
    }
90
91
    /**
92
     * @param string $value
93
     * @return bool|int|null
94
     */
95
    private static function checkByValue(string $value)
96
    {
97
        if ($value === '0') {
98
            return 0;
99
        }
100
101
        if ($value === 'yes') {
102
            return true;
103
        }
104
105
        if ($value === 'no') {
106
            return false;
107
        }
108
109
        if (preg_match('/^[1-9][0-9]*$/', $value) && $value < PHP_INT_MAX) {
110
            return (int)$value;
111
        }
112
113
        return null;
114
    }
115
116
    /**
117
     * @param string $line
118
     * @return ExecCommand
119
     */
120
    private static function parseExecLine(string $line): ExecCommand
121
    {
122
        $pattern = "/^{[ ](?<command>.*)[ ]}$/";
123
        preg_match($pattern, $line, $matches);
124
125
        $execCommand = new ExecCommand;
126
        if (!array_key_exists('command', $matches)) {
127
            $execCommand->path = $line;
128
            return $execCommand;
129
        }
130
131
        $explode = array_map('trim', explode(';', $matches['command']));
132
        array_walk($explode, function (string $value, int $key, ExecCommand $execCommand) {
133
            $split = explode(' ', $value);
134
            $array = self::parseExecDetail($split);
135
            if (count($array)) {
136
                $execCommand->{$array[0]} = $array[1];
137
            }
138
        }, $execCommand);
139
140
        return $execCommand;
141
    }
142
143
    /**
144
     * @param array $values
145
     * @return array
146
     */
147
    private static function parseExecDetail(array $values): array
148
    {
149
        $index = $values[0];
150
151
        if (in_array($index, ['path', 'pid', 'code', 'status'])) {
152
            return [
153
                $index,
154
                self::parseValueByContent($values)
155
            ];
156
        }
157
158
        if ($index === 'argv[]') {
159
            array_shift($values);
160
            array_shift($values);
161
            return [
162
                'argv',
163
                implode(' ', $values),
164
            ];
165
        }
166
167
        if ($index === 'start_time') {
168
            return [
169
                'startTime',
170
                $values[1]
171
            ];
172
        }
173
174
        if ($index === 'stop_time') {
175
            return [
176
                'stopTime',
177
                $values[1]
178
            ];
179
        }
180
181
        if ($index === 'ignore_errors') {
182
            return [
183
                'ignoreErrors',
184
                self::parseValueByContent($values)
185
            ];
186
        }
187
188
        return [];
189
    }
190
191
    /**
192
     * @param array $values
193
     * @return array
194
     */
195
    private static function parseEnvironmentLine(array $values): array
196
    {
197
        array_shift($values);
198
        $environments = [];
199
200
        foreach (explode(' ', implode('=', $values)) as $environment) {
201
            $explode = explode('=', $environment);
202
            $environments[$explode[0]] = self::parseValueByContent($explode);
203
        }
204
205
        return $environments;
206
    }
207
208
    /**
209
     * Used by PHPUnit
210
     *
211
     * @return array
212
     */
213
    public static function getArrayTypes(): array
214
    {
215
        return self::$arrayTypes;
216
    }
217
218
    /**
219
     * Used by PHPUnit
220
     *
221
     * @return array
222
     */
223
    public static function getExecTypes(): array
224
    {
225
        return self::$execTypes;
226
    }
227
}
228