Passed
Push — master ( 80b57f...2460a5 )
by Yo
02:49
created

InputTransformer::extractDataFromValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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