ConfigReader   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mirrorConfigValues() 0 13 5
A __construct() 0 4 1
A readFromPackage() 0 13 1
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Patcher;
7
8
use Composer\Package\PackageInterface;
9
10
use Vaimo\ComposerPatches\Config as Keys;
11
12
class ConfigReader
13
{
14
    /**
15
     * @var \Vaimo\ComposerPatches\Interfaces\PackageConfigExtractorInterface
16
     */
17
    private $infoExtractor;
18
19
    /**
20
     * @param \Vaimo\ComposerPatches\Interfaces\PackageConfigExtractorInterface $infoExtractor
21
     */
22
    public function __construct(
23
        \Vaimo\ComposerPatches\Interfaces\PackageConfigExtractorInterface $infoExtractor
24
    ) {
25
        $this->infoExtractor = $infoExtractor;
26
    }
27
28
    public function readFromPackage(PackageInterface $package)
29
    {
30
        $config = array_filter(
31
            (array)$this->infoExtractor->getConfig($package, Keys::CONFIG_ROOT)
32
        );
33
34
        return $this->mirrorConfigValues($config, array(
35
            Keys::PATCHER_FILE_DEV => Keys::DEV_DEFINITIONS_FILE,
36
            Keys::PATCHER_FILE => Keys::DEFINITIONS_FILE,
37
            Keys::PATCHER_SEARCH => Keys::DEFINITIONS_SEARCH,
38
            Keys::PATCHER_SEARCH_DEV => Keys::DEV_DEFINITIONS_SEARCH,
39
            Keys::PATCHER_TARGETS => Keys::PATCHES_DEPENDS,
40
            Keys::PATCHER_BASE_PATHS => Keys::PATCHES_BASE,
41
        ));
42
    }
43
44
    private function mirrorConfigValues(array $config, array $keyMap)
45
    {
46
        if (isset($config[Keys::PATCHER_CONFIG_ROOT])) {
47
            $patcherConfig = (array)$config[Keys::PATCHER_CONFIG_ROOT];
48
49
            foreach ($keyMap as $source => $target) {
50
                if (!isset($config[$target]) && isset($patcherConfig[$source])) {
51
                    $config[$target] = $patcherConfig[$source];
52
                }
53
            }
54
        }
55
56
        return $config;
57
    }
58
}
59