Completed
Push — 3.0 ( 46e3af...f23b72 )
by Daniel
03:20
created

ProjectConfig::getModuleSpecificSortValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.576

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 6
cts 10
cp 0.6
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 3.576
1
<?php
2
/**
3
 *
4
 *
5
 *
6
 *
7
 */
8
9
namespace MagentoHackathon\Composer\Magento;
10
11
use Composer\Factory;
12
use Composer\Json\JsonFile;
13
use Composer\Json\JsonManipulator;
14
use SebastianBergmann\Exporter\Exception;
15
16
class ProjectConfig
17
{
18
    // Config Keys
19
    const EXTRA_KEY = 'extra';
20
21
    const SORT_PRIORITY_KEY = 'magento-deploy-sort-priority';
22
23
    const MAGENTO_ROOT_DIR_KEY = 'magento-root-dir';
24
25
    const MAGENTO_PROJECT_KEY = 'magento-project';
26
27
    const MAGENTO_DEPLOY_STRATEGY_KEY = 'magento-deploystrategy';
28
    const MAGENTO_DEPLOY_STRATEGY_OVERWRITE_KEY = 'magento-deploystrategy-overwrite';
29
    const MAGENTO_MAP_OVERWRITE_KEY = 'magento-map-overwrite';
30
    const MAGENTO_DEPLOY_IGNORE_KEY = 'magento-deploy-ignore';
31
32
    const MAGENTO_FORCE_KEY = 'magento-force';
33
34
    const AUTO_APPEND_GITIGNORE_KEY = 'auto-append-gitignore';
35
36
    const PATH_MAPPINGS_TRANSLATIONS_KEY = 'path-mapping-translations';
37
38
    const INCLUDE_ROOT_PACKAGE_KEY = 'include-root-package';
39
40
    // Default Values
41
    const DEFAULT_MAGENTO_ROOT_DIR = 'root';
42
43
    const EXTRA_WITH_BOOTSTRAP_PATCH_KEY = 'with-bootstrap-patch';
44
45
    const EXTRA_WITH_SKIP_SUGGEST_KEY = 'skip-suggest-repositories';
46
47
    protected $libraryPath;
48
    protected $libraryPackages;
49
    protected $extra;
50
    protected $composerConfig;
51
52
    /**
53
     * @param array $extra
54
     * @param array $composerConfig
55
     */
56 32
    public function __construct(array $extra, array $composerConfig)
57
    {
58 32
        $this->extra = $extra;
59 32
        $this->composerConfig = $composerConfig;
60
61 32
        if (!is_null($projectConfig = $this->fetchVarFromConfigArray($this->extra, self::MAGENTO_PROJECT_KEY))) {
62
            $this->applyMagentoConfig($projectConfig);
63
        }
64 32
    }
65
66
    /**
67
     * @param array $array
68
     * @param string|integer $key
69
     * @param mixed $default
70
     *
71
     * @return mixed
72
     */
73 32
    protected function fetchVarFromConfigArray($array, $key, $default = null)
74
    {
75 32
        $array = (array)$array;
76 32
        $result = $default;
77 32
        if (isset($array[$key])) {
78 24
            $result = $array[$key];
79 24
        }
80
81 32
        return $result;
82
    }
83
84
    /**
85
     * @param      $key
86
     * @param null $default
87
     *
88
     * @return null
89
     */
90 29
    protected function fetchVarFromExtraConfig($key, $default = null)
91
    {
92 29
        return $this->fetchVarFromConfigArray($this->extra, $key, $default);
93
    }
94
95
    /**
96
     * @param $config
97
     */
98
    protected function applyMagentoConfig($config)
99
    {
100
        $this->libraryPath = $this->fetchVarFromConfigArray($config, 'libraryPath');
101
        $this->libraryPackages = $this->fetchVarFromConfigArray($config, 'libraries');
102
    }
103
104
    /**
105
     * @return mixed
106
     */
107 3
    public function getLibraryPath()
108
    {
109 3
        return $this->libraryPath;
110
    }
111
112
    /**
113
     * @param $packagename
114
     *
115
     * @return null
116
     */
117
    public function getLibraryConfigByPackagename($packagename)
118
    {
119
        return $this->fetchVarFromConfigArray($this->libraryPackages, $packagename);
120
    }
121
122
    /**
123
     * @return string
124
     */
125 20
    public function getMagentoRootDir()
126
    {
127 20
        return rtrim(
128 20
            trim(
129 20
                $this->fetchVarFromExtraConfig(
130 20
                    self::MAGENTO_ROOT_DIR_KEY,
131
                    self::DEFAULT_MAGENTO_ROOT_DIR
132 20
                )
133 20
            ),
134
            DIRECTORY_SEPARATOR
135 20
        );
136
    }
137
138
    /**
139
     * @param $rootDir
140
     */
141
    public function setMagentoRootDir($rootDir)
142
    {
143
        $this->updateExtraConfig(self::MAGENTO_ROOT_DIR_KEY, rtrim(trim($rootDir), DIRECTORY_SEPARATOR));
144
    }
145
146
    /**
147
     * @return bool
148
     */
149 3
    public function hasMagentoRootDir()
150
    {
151 3
        return $this->hasExtraField(self::MAGENTO_ROOT_DIR_KEY);
152
    }
153
154
    public function getMagentoVarDir()
155
    {
156
        return $this->getMagentoRootDir().'var'.DIRECTORY_SEPARATOR;
157
    }
158
159
    /**
160
     * @param $deployStrategy
161
     */
162
    public function setDeployStrategy($deployStrategy)
163
    {
164
        $this->updateExtraConfig(self::MAGENTO_DEPLOY_STRATEGY_KEY, trim($deployStrategy));
165
    }
166
167
    /**
168
     * @return string
169
     */
170 11
    public function getDeployStrategy()
171
    {
172 11
        return trim((string)$this->fetchVarFromExtraConfig(self::MAGENTO_DEPLOY_STRATEGY_KEY));
173
    }
174
175
    /**
176
     * @return bool
177
     */
178
    public function hasDeployStrategy()
179
    {
180
        return $this->hasExtraField(self::MAGENTO_DEPLOY_STRATEGY_KEY);
181
    }
182
183
    /**
184
     * @return array
185
     */
186 6
    public function getDeployStrategyOverwrite()
187
    {
188 6
        return (array)$this->transformArrayKeysToLowerCase(
189 6
            $this->fetchVarFromExtraConfig(self::MAGENTO_DEPLOY_STRATEGY_OVERWRITE_KEY, array())
190 6
        );
191
    }
192
193
    /**
194
     * @return bool
195
     */
196 6
    public function hasDeployStrategyOverwrite()
197
    {
198 6
        return $this->hasExtraField(self::MAGENTO_DEPLOY_STRATEGY_OVERWRITE_KEY);
199
    }
200
201
    /**
202
     * @param $packagename
203
     *
204
     * @return integer
205
     */
206 5
    public function getModuleSpecificDeployStrategy($packagename)
207
    {
208 5
        $moduleSpecificDeployStrategies = $this->getDeployStrategyOverwrite();
209
210 5
        $strategyName = $this->getDeployStrategy();
211 5
        if (isset($moduleSpecificDeployStrategies[$packagename])) {
212
            $strategyName = $moduleSpecificDeployStrategies[$packagename];
213
        }
214 5
        return $strategyName;
215
    }
216
217
    /**
218
     * @param $packagename
219
     *
220
     * @return integer
221
     */
222 1
    public function getModuleSpecificSortValue($packagename)
223
    {
224 1
        $sortPriorityArray = $this->fetchVarFromExtraConfig(self::SORT_PRIORITY_KEY, array());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $sortPriorityArray is correct as $this->fetchVarFromExtra..._PRIORITY_KEY, array()) (which targets MagentoHackathon\Compose...tchVarFromExtraConfig()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
225 1
        if (isset($sortPriorityArray[$packagename])) {
226
            $sortValue = $sortPriorityArray[$packagename];
227
        } else {
228 1
            $sortValue = 100;
229 1
            if ($this->getModuleSpecificDeployStrategy($packagename) === 'copy') {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $this->getModuleSpecific...yStrategy($packagename) (integer) and 'copy' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
230
                $sortValue++;
231
            }
232
        }
233 1
        return $sortValue;
234
    }
235
236
    /**
237
     * @return array
238
     */
239
    public function getMagentoDeployIgnore()
240
    {
241
        return (array)$this->transformArrayKeysToLowerCase(
242
            $this->fetchVarFromExtraConfig(self::MAGENTO_DEPLOY_IGNORE_KEY)
243
        );
244
    }
245
246
    /**
247
     * @param $packagename
248
     *
249
     * @return array
250
     */
251 11
    public function getModuleSpecificDeployIgnores($packagename)
252
    {
253 11
        $moduleSpecificDeployIgnores = array();
254 11
        if ($this->hasMagentoDeployIgnore()) {
255
            $magentoDeployIgnore = $this->getMagentoDeployIgnore();
256
            if (isset($magentoDeployIgnore['*'])) {
257
                $moduleSpecificDeployIgnores = $magentoDeployIgnore['*'];
258
            }
259
            if (isset($magentoDeployIgnore[$packagename])) {
260
                $moduleSpecificDeployIgnores = array_merge(
261
                    $moduleSpecificDeployIgnores,
262
                    $magentoDeployIgnore[$packagename]
263
                );
264
            }
265
        }
266 11
        return $moduleSpecificDeployIgnores;
267
    }
268
269
    /**
270
     * @return bool
271
     */
272 11
    public function hasMagentoDeployIgnore()
273
    {
274 11
        return $this->hasExtraField(self::MAGENTO_DEPLOY_IGNORE_KEY);
275
    }
276
277
    /**
278
     * @param $magentoForce
279
     */
280
    public function setMagentoForce($magentoForce)
281
    {
282
        $this->updateExtraConfig(self::MAGENTO_FORCE_KEY, trim($magentoForce));
283
    }
284
285
    /**
286
     * @return string
287
     */
288 11
    public function getMagentoForce()
289
    {
290 11
        return (bool)$this->fetchVarFromExtraConfig(self::MAGENTO_FORCE_KEY);
291
    }
292
293
    /**
294
     * @return bool
295
     */
296
    public function hasMagentoForce()
297
    {
298
        return $this->hasExtraField(self::MAGENTO_FORCE_KEY);
299
    }
300
301 11
    public function getMagentoForceByPackageName($packagename)
0 ignored issues
show
Unused Code introduced by
The parameter $packagename is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
302
    {
303 11
        return $this->getMagentoForce();
304
    }
305
306
    /**
307
     * @return bool
308
     */
309 6
    public function hasAutoAppendGitignore()
310
    {
311 6
        return $this->hasExtraField(self::AUTO_APPEND_GITIGNORE_KEY);
312
    }
313
314
    /**
315
     * @return array
316
     */
317 1
    public function getPathMappingTranslations()
318
    {
319 1
        return (array)$this->fetchVarFromExtraConfig(self::PATH_MAPPINGS_TRANSLATIONS_KEY);
320
    }
321
322
    /**
323
     * @return bool
324
     */
325 2
    public function hasPathMappingTranslations()
326
    {
327 2
        return $this->hasExtraField(self::PATH_MAPPINGS_TRANSLATIONS_KEY);
328
    }
329
330
    /**
331
     * @return array
332
     */
333
    public function getMagentoDeployOverwrite()
334
    {
335
        return (array)$this->transformArrayKeysToLowerCase(
336
            $this->fetchVarFromExtraConfig(self::MAGENTO_DEPLOY_STRATEGY_OVERWRITE_KEY)
337
        );
338
    }
339
340 5
    public function getMagentoMapOverwrite()
341
    {
342 5
        return $this->transformArrayKeysToLowerCase(
343 5
            (array)$this->fetchVarFromExtraConfig(self::MAGENTO_MAP_OVERWRITE_KEY)
344 5
        );
345
    }
346 19
    protected function hasExtraField($key)
347
    {
348 19
        return (bool)!is_null($this->fetchVarFromExtraConfig($key));
349
    }
350
351
    /**
352
     * @param $key
353
     * @param $value
354
     */
355
    protected function updateExtraConfig($key, $value)
356
    {
357
        $this->extra[$key] = $value;
358
        $this->updateExtraJson();
359
    }
360
361
    /**
362
     * @throws \Exception
363
     */
364
    protected function updateExtraJson()
365
    {
366
        $composerFile = Factory::getComposerFile();
367
368
        if (!file_exists($composerFile) && !file_put_contents($composerFile, "{\n}\n")) {
369
            throw new Exception(sprintf('%s could not be created', $composerFile));
370
        }
371
372
        if (!is_readable($composerFile)) {
373
            throw new Exception(sprintf('%s is not readable', $composerFile));
374
        }
375
376
        if (!is_writable($composerFile)) {
377
            throw new Exception(sprintf('%s is not writable', $composerFile));
378
        }
379
380
        $json = new JsonFile($composerFile);
381
        $composer = $json->read();
382
383
        $baseExtra = array_key_exists(self::EXTRA_KEY, $composer)
384
            ? $composer[self::EXTRA_KEY]
385
            : array();
386
387
        if (!$this->updateFileCleanly($json, $baseExtra, $this->extra, self::EXTRA_KEY)) {
388
            foreach ($this->extra as $key => $value) {
389
                $baseExtra[$key] = $value;
390
            }
391
392
            $composer[self::EXTRA_KEY] = $baseExtra;
393
            $json->write($composer);
394
        }
395
    }
396
397
    /**
398
     * @param JsonFile $json
399
     * @param array    $base
400
     * @param array    $new
401
     * @param          $rootKey
402
     *
403
     * @return bool
404
     */
405
    private function updateFileCleanly(JsonFile $json, array $base, array $new, $rootKey)
0 ignored issues
show
Unused Code introduced by
The parameter $base is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
406
    {
407
        $contents = file_get_contents($json->getPath());
408
409
        $manipulator = new JsonManipulator($contents);
410
411
        foreach ($new as $childKey => $childValue) {
412
            if (!$manipulator->addLink($rootKey, $childKey, $childValue)) {
413
                return false;
414
            }
415
        }
416
417
        file_put_contents($json->getPath(), $manipulator->getContents());
418
419
        return true;
420
    }
421
422
    /**
423
     * @param array $array
424
     *
425
     * @return array
426
     */
427 11
    public function transformArrayKeysToLowerCase(array $array)
428
    {
429 11
        return array_change_key_case($array, CASE_LOWER);
430
    }
431
432 5
    public function getComposerRepositories()
433
    {
434 5
        return $this->fetchVarFromConfigArray($this->composerConfig, 'repositories', array());
435
    }
436
437
    /**
438
     * Get Composer vendor directory
439
     *
440
     * @return string
441
     */
442 7
    public function getVendorDir()
443
    {
444 7
        return $this->fetchVarFromConfigArray(
445 7
            isset($this->composerConfig['config']) ? $this->composerConfig['config'] : array(),
446 7
            'vendor-dir',
447 7
            getcwd() . '/vendor'
448 7
        );
449
    }
450
451
    /**
452
     * @return boolean
453
     */
454 8
    public function mustApplyBootstrapPatch()
455
    {
456 8
        return (bool) $this->fetchVarFromExtraConfig(self::EXTRA_WITH_BOOTSTRAP_PATCH_KEY, true);
457
    }
458
459
    /**
460
     * @return boolean
461
     */
462 6
    public function skipSuggestComposerRepositories()
463
    {
464 6
        return (bool) $this->fetchVarFromExtraConfig(self::EXTRA_WITH_SKIP_SUGGEST_KEY, false);
465
    }
466
467
    /**
468
     * @param $includeRootPackage
469
     */
470
    public function setIncludeRootPackage($includeRootPackage)
471
    {
472
        $this->updateExtraConfig(self::INCLUDE_ROOT_PACKAGE_KEY, trim($includeRootPackage));
473
    }
474
475
    /**
476
     * @return bool
477
     */
478 1
    public function getIncludeRootPackage()
479
    {
480 1
        return (bool)$this->fetchVarFromExtraConfig(self::INCLUDE_ROOT_PACKAGE_KEY);
481
    }
482
}
483