Collector   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A applySharedConfig() 0 23 6
A updatePackagePatchesConfig() 0 5 1
A __construct() 0 11 1
A applyListManipulators() 0 13 1
A collect() 0 38 4
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Patch;
7
8
use Composer\Package\RootPackage;
9
use Composer\Package\PackageInterface;
10
11
use Vaimo\ComposerPatches\Interfaces\PatchSourceLoaderInterface;
12
use Vaimo\ComposerPatches\Patch\Definition as PatchDefinition;
13
14
class Collector
15
{
16
    /**
17
     * @var \Vaimo\ComposerPatches\Patch\ListNormalizer
18
     */
19
    private $listNormalizer;
20
21
    /**
22
     * @var PatchSourceLoaderInterface[]
23
     */
24
    private $sourceLoaders;
25
26
    /**
27
     * @var \Vaimo\ComposerPatches\Patcher\ConfigReader
28
     */
29
    private $patcherConfigReader;
30
31
    /**
32
     * @var \Vaimo\ComposerPatches\Patch\DefinitionList\Updater
33
     */
34
    private $patchListUpdater;
35
36
    /**
37
     * @param \Vaimo\ComposerPatches\Patch\ListNormalizer $listNormalizer
38
     * @param \Vaimo\ComposerPatches\Patcher\ConfigReader $patcherConfigReader
39
     * @param PatchSourceLoaderInterface[] $sourceLoaders
40
     */
41
    public function __construct(
42
        \Vaimo\ComposerPatches\Patch\ListNormalizer $listNormalizer,
43
        \Vaimo\ComposerPatches\Patcher\ConfigReader $patcherConfigReader,
44
        array $sourceLoaders
45
    ) {
46
        $this->listNormalizer = $listNormalizer;
47
        $this->sourceLoaders = $sourceLoaders;
48
49
        $this->patcherConfigReader = $patcherConfigReader;
50
51
        $this->patchListUpdater = new \Vaimo\ComposerPatches\Patch\DefinitionList\Updater();
52
    }
53
54
    /**
55
     * @param \Composer\Package\PackageInterface[] $packages
56
     * @return array
57
     */
58
    public function collect(array $packages)
59
    {
60
        $patchesByOwner = array();
61
62
        foreach ($packages as $package) {
63
            $ownerName = $package->getName();
64
65
            $config = $this->patcherConfigReader->readFromPackage($package);
66
67
            /** @var PatchSourceLoaderInterface[] $sourceLoaders */
68
            $sourceLoaders = array_intersect_key($this->sourceLoaders, $config);
69
            $ownerConfig = array_diff_key($config, $this->sourceLoaders);
70
71
            $loadedPatches = array();
72
73
            if (empty($sourceLoaders)) {
74
                continue;
75
            }
76
77
            foreach ($sourceLoaders as $loaderName => $source) {
78
                $resultGroups = $source->load($package, $config[$loaderName]);
79
80
                $loadedPatches[$loaderName] = $this->applyListManipulators(
81
                    $resultGroups,
82
                    $ownerConfig
83
                );
84
            }
85
86
            $patches = array_reduce(
87
                array_reduce($loadedPatches, 'array_merge', array()),
88
                'array_merge_recursive',
89
                array()
90
            );
91
92
            $patchesByOwner[$ownerName] = $this->updatePackagePatchesConfig($package, $patches);
93
        }
94
95
        return array_reduce($patchesByOwner, 'array_merge_recursive', array());
96
    }
97
98
    private function applyListManipulators(array $resultGroups, array $ownerConfig)
99
    {
100
        $normalizer = $this->listNormalizer;
101
102
        $that = $this;
103
104
        return array_map(
105
            function (array $results) use ($that, $ownerConfig, $normalizer) {
106
                $normalizedList = $normalizer->normalize($results, $ownerConfig);
107
108
                return $that->applySharedConfig($results, $normalizedList);
109
            },
110
            $resultGroups
111
        );
112
    }
113
114
    public function applySharedConfig(array $configOrigin, array $patches)
115
    {
116
        $baseConfig = isset($configOrigin['_config']) ? $configOrigin['_config'] : array();
117
118
        foreach ($configOrigin as $target => $items) {
119
            $updates = array_replace(
120
                $baseConfig,
121
                isset($items['_config']) ? $items['_config'] : array()
122
            );
123
124
            if (empty($updates) || !isset($patches[$target])) {
125
                continue;
126
            }
127
128
            $patches[$target] = array_map(
129
                function ($config) use ($updates) {
130
                    return array_replace($config, $updates);
131
                },
132
                $patches[$target]
133
            );
134
        }
135
136
        return $patches;
137
    }
138
139
    private function updatePackagePatchesConfig(PackageInterface $package, array $patches)
140
    {
141
        return $this->patchListUpdater->embedInfoToItems($patches, array(
142
            PatchDefinition::OWNER => $package->getName(),
143
            PatchDefinition::OWNER_IS_ROOT => $package instanceof RootPackage
144
        ));
145
    }
146
}
147