Passed
Push — master ( 2aad05...4fe473 )
by Yo
02:45
created

InputTransformer::createConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

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