ConfigFactory::create()   B
last analyzed

Complexity

Conditions 8
Paths 18

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 27
nc 18
nop 1
dl 0
loc 46
rs 8.4444
c 2
b 0
f 0
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Factories;
7
8
use Vaimo\ComposerPatches\Config as PluginConfig;
9
use Vaimo\ComposerPatches\Environment;
10
11
class ConfigFactory
12
{
13
    /**
14
     * @var \Vaimo\ComposerPatches\Composer\Context
15
     */
16
    private $composerContext;
17
18
    /**
19
     * @var \Vaimo\ComposerPatches\Config\Defaults
20
     */
21
    private $defaultsProvider;
22
23
    /**
24
     * @var \Vaimo\ComposerPatches\Utils\ConfigUtils
25
     */
26
    private $configUtils;
27
28
    /**
29
     * @var \Vaimo\ComposerPatches\Config\Context
30
     */
31
    private $context;
32
33
    /**
34
     * @var array
35
     */
36
    private $defaults;
37
38
    /**
39
     * @var \Vaimo\ComposerPatches\Utils\DataUtils
40
     */
41
    private $dataUtils;
42
43
    /**
44
     * @param \Vaimo\ComposerPatches\Composer\Context $composerContext
45
     * @param array $defaults
46
     */
47
    public function __construct(
48
        \Vaimo\ComposerPatches\Composer\Context $composerContext,
49
        array $defaults = array()
50
    ) {
51
        $this->composerContext = $composerContext;
52
        $this->defaults = $defaults;
53
54
        $this->defaultsProvider = new \Vaimo\ComposerPatches\Config\Defaults();
55
        $this->configUtils = new \Vaimo\ComposerPatches\Utils\ConfigUtils();
56
        $this->context = new \Vaimo\ComposerPatches\Config\Context();
57
        $this->dataUtils = new \Vaimo\ComposerPatches\Utils\DataUtils();
58
    }
59
60
    public function create(array $configSources = array())
61
    {
62
        $defaults = array_replace(
63
            $this->defaultsProvider->getPatcherConfig(),
64
            $this->defaults,
65
            array_filter(array(PluginConfig::PATCHER_GRACEFUL => (bool)getenv(Environment::GRACEFUL_MODE)))
66
        );
67
68
        $composer = $this->composerContext->getLocalComposer();
69
70
        $extra = $composer->getPackage()->getExtra();
71
72
        if (isset($extra['patcher-config']) && !isset($extra[PluginConfig::PATCHER_CONFIG_ROOT])) {
73
            $extra[PluginConfig::PATCHER_CONFIG_ROOT] = $extra['patcher-config'];
74
        }
75
76
        $subConfigKeys = array(
77
            $this->context->getOperationSystemTypeCode(),
78
            $this->context->getOperationSystemName(),
79
            $this->context->getOperationSystemFamily(),
80
            '',
81
        );
82
83
        foreach (array_unique($subConfigKeys) as $key) {
84
            $configRootKey = PluginConfig::PATCHER_CONFIG_ROOT . ($key ? ('-' . $key) : '');
85
86
            $patcherConfig = $this->resolvePatcherConfigBase($extra, $configRootKey);
87
88
            if (isset($patcherConfig['patchers']) && !isset($patcherConfig[PluginConfig::PATCHER_APPLIERS])) {
89
                $patcherConfig[PluginConfig::PATCHER_APPLIERS] = $patcherConfig['patchers'];
90
                unset($patcherConfig['patchers']);
91
            }
92
93
            if ($patcherConfig) {
94
                array_unshift($configSources, $patcherConfig);
95
            }
96
        }
97
98
        $config = array_reduce(
99
            $configSources,
100
            array($this->configUtils, 'mergeApplierConfig'),
101
            $defaults
102
        );
103
104
        return new PluginConfig(
105
            $this->resolveValidSubOperations($config, $subConfigKeys)
106
        );
107
    }
108
109
    private function resolvePatcherConfigBase(array $extra, $rootKey)
110
    {
111
        $patcherConfig = isset($extra[$rootKey]) ? $extra[$rootKey] : array();
112
113
        if ($patcherConfig === false) {
114
            $patcherConfig = array(
115
                PluginConfig::PATCHER_SOURCES => false
116
            );
117
        }
118
119
        if (!isset($patcherConfig[PluginConfig::PATCHER_SOURCES])) {
120
            if (isset($extra['enable-patching']) && !$extra['enable-patching']) {
121
                $patcherConfig[PluginConfig::PATCHER_SOURCES] = false;
122
            } elseif (isset($extra['enable-patching-from-packages']) && !$extra['enable-patching-from-packages']) {
123
                $patcherConfig[PluginConfig::PATCHER_SOURCES] = array('packages' => false, 'vendors' => false);
124
            }
125
        }
126
127
        return $patcherConfig;
128
    }
129
130
    private function resolveValidSubOperations(array $config, array $subConfigKeys)
131
    {
132
        $subOperationKeys = array_merge(
133
            array_filter($subConfigKeys),
134
            array(PluginConfig::OS_DEFAULT)
135
        );
136
137
        $baseOperations = $config[PluginConfig::PATCHER_APPLIERS][PluginConfig::APPLIER_DEFAULT];
138
139
        foreach ($config[PluginConfig::PATCHER_APPLIERS] as $applierCode => $operations) {
140
            if ($applierCode === PluginConfig::APPLIER_DEFAULT) {
141
                continue;
142
            }
143
144
            $operations = array_replace($baseOperations, $operations);
145
146
            foreach ($operations as $opCode => $operation) {
147
                if (!is_array($operation)) {
148
                    continue;
149
                }
150
151
                if (array_filter($operation, 'is_numeric')) {
152
                    continue;
153
                }
154
155
                $subOperations = $this->dataUtils->extractOrderedItems($operation, $subOperationKeys);
156
157
                if (empty($subOperations)) {
158
                    continue;
159
                }
160
161
                $config[PluginConfig::PATCHER_APPLIERS][$applierCode][$opCode] = reset($subOperations);
162
            }
163
        }
164
165
        return $config;
166
    }
167
}
168