Test Failed
Pull Request — master (#10)
by Yo
02:35
created

InputTransformer::createConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

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