Completed
Pull Request — master (#18)
by Yo
02:49
created

InputTransformer::createConfigurationFile()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 50
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 4.0001

Importance

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