Passed
Branch master (4a352c)
by Allan
04:38 queued 02:31
created

Transformer::createDetailedList()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 3
nop 1
dl 0
loc 35
rs 8.6666
c 0
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\Patch\DefinitionList;
7
8
use Vaimo\ComposerPatches\Patch\Definition as Patch;
9
10
class Transformer
11
{
12
    public function createSimplifiedList(array $patches)
13
    {
14
        $groups = $this->createTargetsList($patches);
15
16
        $result = array_map(function ($group) {
17
            $fingerprints = array_map(function ($item) {
18
                return sprintf(
19
                    '%s, %s:%s',
20
                    isset($item[Patch::LABEL]) ? $item[Patch::LABEL] : '{no label}',
21
                    Patch::HASH,
22
                    isset($item[Patch::HASH]) && $item[Patch::HASH] ? $item[Patch::HASH] : '{no hash}'
23
                );
24
            }, $group);
25
26
            $keys = array_map(function ($key, $item) {
27
                return sprintf('%s%s%s', $item[Patch::OWNER], Patch::SOURCE_INFO_SEPARATOR, $key);
28
            }, array_keys($group), $group);
29
30
            return array_combine($keys, $fingerprints);
31
32
        }, $groups);
33
34
        return $result;
35
    }
36
37
    public function createDetailedList(array $patches)
38
    {
39
        $result = array();
40
41
        $labelInfoMatcher = sprintf('/%s:(?P<hash>.*)/', Patch::HASH);
42
43
        foreach ($patches as $target => $group) {
44
            $result[$target] = array();
45
46
            if (!is_array($group)) {
47
                continue;
48
            }
49
50
            foreach ($group as $sourceInfo => $label) {
51
                $sourceConfig = explode(Patch::SOURCE_INFO_SEPARATOR, $sourceInfo);
52
53
                $path = array_pop($sourceConfig);
54
                $owner = array_pop($sourceConfig);
55
56
                $labelConfig = explode(',', $label);
57
58
                preg_match($labelInfoMatcher, trim(end($labelConfig)), $matches);
59
60
                $result[$target][$path] = array(
61
                    'path' => $path,
62
                    'targets' => array($target),
63
                    'source' => $path,
64
                    'owner' => $owner ? $owner : Patch::OWNER_UNKNOWN,
65
                    'label' => implode(',', array_slice($labelConfig, 0, -1)),
66
                    'md5' => is_array($matches) && isset($matches['hash']) ? $matches['hash'] : null
67
                );
68
            }
69
        }
70
71
        return $result;
72
    }
73
74
    public function createTargetsList(array $patches)
75
    {
76
        $result = array();
77
78
        foreach ($patches as $originName => $patchGroup) {
79
            if (!is_array($patchGroup)) {
80
                continue;
81
            }
82
83
            foreach ($patchGroup as $patchPath => $patchInfo) {
84
                foreach ($patchInfo[Patch::TARGETS] as $target) {
85
                    if (!isset($result[$target])) {
86
                        $result[$target] = array();
87
                    }
88
89
                    $path = (isset($patchInfo['url']) && $patchInfo['url']) ? $patchInfo['url'] : $patchPath;
90
91
                    $result[$target][$path] = array_replace(
92
                        $patchInfo,
93
                        array(Patch::ORIGIN => $originName)
94
                    );
95
                }
96
            }
97
        }
98
99
        return $result;
100
    }
101
102
    public function groupItemsByTarget(array $patchesList)
103
    {
104
        $result = array();
105
106
        foreach ($patchesList as $origin => $group) {
107
            if (!isset($result[$origin])) {
108
                $result[$origin] = array();
109
            }
110
111
            foreach ($group as $path => $patch) {
112
                foreach ($patch[Patch::TARGETS] as $target) {
113
                    $result[$target][$path] = array_replace(
114
                        $patch,
115
                        array(Patch::ORIGIN => $origin)
116
                    );
117
                }
118
            }
119
        }
120
121
        return array_filter($result);
122
    }
123
124
    public function createOriginList(array $patchesList)
125
    {
126
        $result = array();
127
128
        foreach ($patchesList as $group) {
129
            foreach ($group as $path => $patch) {
130
                $origin = $patch[Patch::ORIGIN];
131
132
                if (!isset($result[$origin])) {
133
                    $result[$origin] = array();
134
                }
135
136
                if (isset($result[$origin][$path])) {
137
                    continue;
138
                }
139
140
                $result[$origin][$path] = array_diff_key($patch, array(Patch::ORIGIN => true));
141
            }
142
        }
143
144
        return array_filter($result);
145
    }
146
}
147