Failed Conditions
Pull Request — master (#18)
by Yo
02:43
created

InputTransformer::extractAutoloads()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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