Completed
Push — master ( 77675a...fe304c )
by Yo
02:42
created

InputTransformer::createConfiguration()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 1

Importance

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