Completed
Push — master ( fe304c...6b259c )
by Yo
02:47
created

InputTransformer::createConfigurationFile()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 72
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 63
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 63
cts 64
cp 0.9844
rs 8.5164
c 0
b 0
f 0
cc 5
eloc 61
nc 5
nop 1
crap 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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