Test Failed
Pull Request — master (#17)
by Yo
02:38
created

InputTransformer::extractProvidedPackages()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.2
ccs 8
cts 8
cp 1
cc 4
eloc 7
nc 2
nop 1
crap 4
1
<?php
2
namespace Yoanm\ComposerConfigManager\Infrastructure\Command\Transformer;
3
4
use Yoanm\ComposerConfigManager\Domain\Model\Author;
5
use Yoanm\ComposerConfigManager\Domain\Model\Autoload;
6
use Yoanm\ComposerConfigManager\Domain\Model\AutoloadEntry;
7
use Yoanm\ComposerConfigManager\Domain\Model\Configuration;
8
use Yoanm\ComposerConfigManager\Domain\Model\ConfigurationFile;
9
use Yoanm\ComposerConfigManager\Domain\Model\Package;
10
use Yoanm\ComposerConfigManager\Domain\Model\Script;
11
use Yoanm\ComposerConfigManager\Domain\Model\SuggestedPackage;
12
use Yoanm\ComposerConfigManager\Domain\Model\Support;
13
14
class InputTransformer
15
{
16
    const SEPARATOR = '#';
17
18
    const KEY_PACKAGE_NAME = 'package-name';
19
    const KEY_TYPE = 'type';
20
    const KEY_LICENSE = 'license';
21
    const KEY_PACKAGE_VERSION = 'package-version';
22
    const KEY_DESCRIPTION = 'description';
23
    const KEY_KEYWORD = 'keyword';
24
    const KEY_AUTHOR = 'author';
25
    const KEY_PROVIDED_PACKAGE = 'provided-package';
26
    const KEY_SUGGESTED_PACKAGE = 'suggested-package';
27
    const KEY_SUPPORT = 'support';
28
    const KEY_AUTOLOAD_PSR0 = 'autoload-psr0';
29
    const KEY_AUTOLOAD_PSR4 = 'autoload-psr4';
30
    const KEY_AUTOLOAD_DEV_PSR0 = 'autoload-dev-psr0';
31
    const KEY_AUTOLOAD_DEV_PSR4 = 'autoload-dev-psr4';
32
    const KEY_REQUIRE = 'require';
33
    const KEY_REQUIRE_DEV = 'require-dev';
34
    const KEY_SCRIPT = 'script';
35
36
    /**
37
     * @param $inputList
38
     *
39
     * @return ConfigurationFile
40 9
     */
41
    public function fromCommandLine($inputList)
42 9
    {
43
        return $this->createConfiguration($inputList);
44
    }
45
46
    /**
47
     * @param array $inputList
48
     *
49
     * @return ConfigurationFile
50 9
     */
51
    protected function createConfiguration(array $inputList)
52 9
    {
53 9
        return new ConfigurationFile(
54 9
            new Configuration(
55 9
                $inputList[self::KEY_PACKAGE_NAME],
56 9
                $this->getValue($inputList, self::KEY_TYPE, null),
57 9
                $this->getValue($inputList, self::KEY_LICENSE, null),
58 9
                $this->getValue($inputList, self::KEY_PACKAGE_VERSION, null),
59 9
                $this->getValue($inputList, self::KEY_DESCRIPTION, null),
60 9
                $this->extractKeywords($inputList),
61 9
                $this->extractAuthors($inputList),
62 9
                $this->extractProvidedPackages($inputList),
63 9
                $this->extractSuggestedPackages($inputList),
64 9
                $this->extractSupports($inputList),
65 9
                $this->extractAutoloads($inputList),
66 9
                $this->extractAutoloadsDev($inputList),
67 9
                $this->extractRequiredPackages($inputList),
68 9
                $this->extractRequiredDevPackages($inputList),
69
                $this->extractScripts($inputList)
70
            ),
71
            array_intersect(
72
                [
73
                    self::KEY_PACKAGE_NAME,
74
                    self::KEY_TYPE,
75
                    self::KEY_LICENSE,
76
                    self::KEY_PACKAGE_VERSION,
77
                    self::KEY_DESCRIPTION,
78 9
                    self::KEY_KEYWORD,
79
                    self::KEY_AUTHOR,
80 9
                    self::KEY_PROVIDED_PACKAGE,
81
                    self::KEY_SUGGESTED_PACKAGE,
82
                    self::KEY_SUPPORT,
83
                    self::KEY_AUTOLOAD_PSR0,
84
                    self::KEY_AUTOLOAD_PSR4,
85
                    self::KEY_AUTOLOAD_DEV_PSR0,
86
                    self::KEY_AUTOLOAD_DEV_PSR4,
87
                    self::KEY_REQUIRE,
88 9
                    self::KEY_REQUIRE_DEV,
89
                    self::KEY_SCRIPT,
90 9
                ],
91 9
                array_keys($inputList)
92 1
            )
93 1
        );
94 1
    }
95 1
96
    /**
97 9
     * @param array  $inputList
98
     * @param string $key
99
     * @param string $defaultValue
100
     *
101
     * @return string
102
     */
103
    protected function getValue(array $inputList, $key, $defaultValue)
104
    {
105 9
        return isset($inputList[$key]) ? $inputList[$key] : $defaultValue;
106
    }
107 9
108 9
    /**
109 1
     * @param array $inputList
110 1
     *
111 1
     * @return array
112 1
     */
113 1
    protected function extractKeywords(array $inputList)
114
    {
115 1
        $list = [];
116 1
        if (isset($inputList[self::KEY_KEYWORD]) && is_array($inputList[self::KEY_KEYWORD])) {
117 1
            foreach ($inputList[self::KEY_KEYWORD] as $keyword) {
118
                $list[] = $keyword;
119 9
            }
120
        }
121
122
        return $list;
123
    }
124
125
    /**
126
     * @param array $inputList
127 9
     *
128
     * @return array
129 9
     */
130 9
    protected function extractAuthors(array $inputList)
131 2
    {
132 2
        $list = [];
133 2
        if (isset($inputList[self::KEY_AUTHOR]) && is_array($inputList[self::KEY_AUTHOR])) {
134 2
            foreach ($inputList[self::KEY_AUTHOR] as $key => $author) {
135 2
                $data = $this->extractDataFromValue($author);
136
                $name = array_shift($data);
137 9
                $email = array_shift($data);
138
                $role = array_shift($data);
139
140
                $list[] = new Author($name, $email, $role);
141
            }
142
        }
143
144
        return $list;
145 9
    }
146
147 9
    /**
148 9
     * @param array $inputList
149 9
     *
150 9
     * @return array
151 2
     */
152 2
    protected function extractProvidedPackages(array $inputList)
153 2
    {
154 2
        $list = [];
155 2
        if (isset($inputList[self::KEY_PROVIDED_PACKAGE]) && is_array($inputList[self::KEY_PROVIDED_PACKAGE])) {
156 2
            foreach ($inputList[self::KEY_PROVIDED_PACKAGE] as $rawValue) {
157 2
                list ($name, $versionConstraint) = $this->extractDataFromValue($rawValue);
158 2
                $list[] = new Package($name, $versionConstraint);
159
            }
160 9
        }
161
162
        return $list;
163
    }
164
165
    /**
166
     * @param array $inputList
167
     *
168 9
     * @return array
169
     */
170 9
    protected function extractSuggestedPackages(array $inputList)
171 9
    {
172 2
        $list = [];
173 2
        if (isset($inputList[self::KEY_SUGGESTED_PACKAGE])
174 2
            && is_array($inputList[self::KEY_SUGGESTED_PACKAGE])
175 2
        ) {
176 2
            foreach ($inputList[self::KEY_SUGGESTED_PACKAGE] as $rawValue) {
177
                $data = $this->extractDataFromValue($rawValue);
178 9
                $list[] = new SuggestedPackage(
179
                    array_shift($data),
180
                    implode(self::SEPARATOR, $data)
181
                );
182
            }
183
        }
184
185
        return $list;
186 9
    }
187
188 9
    /**
189
     * @param array $inputList
190 9
     *
191 2
     * @return array
192 9
     */
193
    protected function extractSupports(array $inputList)
194 9
    {
195 2
        $list = [];
196 9
        if (isset($inputList[self::KEY_SUPPORT]) && is_array($inputList[self::KEY_SUPPORT])) {
197
            foreach ($inputList[self::KEY_SUPPORT] as $rawValue) {
198
                $data = $this->extractDataFromValue($rawValue);
199 9
                $list[] = new Support(array_shift($data), implode(self::SEPARATOR, $data));
200
            }
201
        }
202
203
        return $list;
204
    }
205
206
    /**
207 9
     * @param array $inputList
208
     *
209 9
     * @return array
210
     */
211 9
    protected function extractAutoloads(array $inputList)
212 2
    {
213 9
        $list = [];
214
        // PSR0
215 9
        foreach ($this->extractAutoloadList($inputList, self::KEY_AUTOLOAD_PSR0) as $namespace => $path) {
216 2
            $list[] = new Autoload(Autoload::TYPE_PSR0, $path, $namespace);
217 9
        }
218
        // PSR-4
219 9
        foreach ($this->extractAutoloadList($inputList, self::KEY_AUTOLOAD_PSR4) as $namespace => $path) {
220
            $list[] = new Autoload(Autoload::TYPE_PSR4, $path, $namespace);
221
        }
222
223
224
        return $list;
225
    }
226
227 9
    /**
228
     * @param array $inputList
229 9
     *
230 9
     * @return array
231 2
     */
232 2
    protected function extractAutoloadsDev(array $inputList)
233 2
    {
234 2
        $list = [];
235 2
        // PSR0
236
        foreach ($this->extractAutoloadList($inputList, self::KEY_AUTOLOAD_DEV_PSR0) as $namespace => $path) {
237 9
            $list[] = new Autoload(Autoload::TYPE_PSR0, $path, $namespace);
238
        }
239
        // PSR-4
240
        foreach ($this->extractAutoloadList($inputList, self::KEY_AUTOLOAD_DEV_PSR4) as $namespace => $path) {
241
            $list[] = new Autoload(Autoload::TYPE_PSR4, $path, $namespace);
242
        }
243
244 9
        return $list;
245
    }
246 9
247 9
    /**
248 2
     * @param array $inputList
249 2
     *
250 2
     * @return array
251 2
     */
252 2
    protected function extractRequiredPackages(array $inputList)
253
    {
254 9
        $list = [];
255
        if (isset($inputList[self::KEY_REQUIRE]) && is_array($inputList[self::KEY_REQUIRE])) {
256
            foreach ($inputList[self::KEY_REQUIRE] as $rawValue) {
257
                list ($name, $versionConstraint) = $this->extractDataFromValue($rawValue);
258
                $list[] = new Package($name, $versionConstraint);
259
            }
260
        }
261
262 9
        return $list;
263
    }
264 9
    /**
265 9
     * @param array $inputList
266 2
     *
267 2
     * @return array
268 2
     */
269 2
    protected function extractRequiredDevPackages(array $inputList)
270 2
    {
271
        $list = [];
272 9
        if (isset($inputList[self::KEY_REQUIRE_DEV]) && is_array($inputList[self::KEY_REQUIRE_DEV])) {
273
            foreach ($inputList[self::KEY_REQUIRE_DEV] as $rawValue) {
274
                list ($name, $versionConstraint) = $this->extractDataFromValue($rawValue);
275
                $list[] = new Package($name, $versionConstraint);
276
            }
277
        }
278
279
        return $list;
280 9
    }
281
282 9
    /**
283
     * @param array $inputList
284
     *
285
     * @return array
286
     */
287
    protected function extractScripts(array $inputList)
288
    {
289
        $list = [];
290
        if (isset($inputList[self::KEY_SCRIPT]) && is_array($inputList[self::KEY_SCRIPT])) {
291 9
            foreach ($inputList[self::KEY_SCRIPT] as $rawValue) {
292
                list ($name, $command) = $this->extractDataFromValue($rawValue);
293 9
                $list[] = new Script($name, $command);
294 9
            }
295 3
        }
296 3
297 3
        return $list;
298 3
    }
299 3
300
    /**
301 9
     * @param string $value
302
     *
303
     * @return array
304
     */
305
    protected function extractDataFromValue($value)
306
    {
307
        return explode(self::SEPARATOR, $value);
308
    }
309
310
    /**
311
     * @param array  $inputList
312
     * @param string $optionKey
313
     *
314
     * @return array
315
     */
316
    protected function extractAutoloadList(array $inputList, $optionKey)
317
    {
318
        $list = [];
319
        if (isset($inputList[$optionKey]) && is_array($inputList[$optionKey])) {
320
            foreach ($inputList[$optionKey] as $rawValue) {
321
                list ($namespace, $path) = $this->extractDataFromValue($rawValue);
322
                $list[$namespace] = $path;
323
            }
324
        }
325
326
        return $list;
327
    }
328
}
329