Passed
Push — master ( f9f6c5...65f41a )
by Allan
04:35
created

TargetsResolverComponent::createError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
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\LoaderComponents;
7
8
use Vaimo\ComposerPatches\Patch\Definition as PatchDefinition;
9
10
class TargetsResolverComponent implements \Vaimo\ComposerPatches\Interfaces\DefinitionListLoaderComponentInterface
11
{
12
    /**
13
     * @var \Vaimo\ComposerPatches\Package\InfoResolver
14
     */
15
    private $packageInfoResolver;
16
17
    /**
18
     * @var \Vaimo\ComposerPatches\Patch\File\Loader
19
     */
20
    private $patchFileLoader;
21
    
22
    /**
23
     * @var \Vaimo\ComposerPatches\Patch\File\Analyser
24
     */
25
    private $patchFileAnalyser;
26
27
    /**
28
     * @var bool
29
     */
30
    private $gracefulMode;
31
32
    /**
33
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
34
     *
35
     * @param \Vaimo\ComposerPatches\Package\InfoResolver $packageInfoResolver
36
     * @param bool $gracefulMode
37
     */
38
    public function __construct(
39
        \Vaimo\ComposerPatches\Package\InfoResolver $packageInfoResolver,
40
        $gracefulMode = false
41
    ) {
42
        $this->packageInfoResolver = $packageInfoResolver;
43
        $this->gracefulMode = $gracefulMode;
44
45
        $this->patchFileLoader = new \Vaimo\ComposerPatches\Patch\File\Loader();
46
        $this->patchFileAnalyser = new \Vaimo\ComposerPatches\Patch\File\Analyser();
47
    }
48
49
    /**
50
     * @param array $patches
51
     * @param \Composer\Package\PackageInterface[] $packagesByName
52
     * @return array
53
     * @throws \Vaimo\ComposerPatches\Exceptions\LoaderException
54
     */
55
    public function process(array $patches, array $packagesByName)
56
    {
57
        foreach ($patches as $patchTarget => $packagePatches) {
58
            foreach ($packagePatches as $index => $info) {
59
                $targets = isset($info[PatchDefinition::TARGETS])
60
                    ? $info[PatchDefinition::TARGETS]
61
                    : array();
62
63
                if (!in_array(PatchDefinition::BUNDLE_TARGET, $targets, true)) {
64
                    continue;
65
                }
66
                
67
                if (count($targets) > 1) {
68
                    continue;
69
                }
70
71
                $path = $info[PatchDefinition::PATH];
72
                $source = $info[PatchDefinition::SOURCE];
73
74
                if (!file_exists($path)) {
75
                    throw $this->createError('patch file not found', $source);
76
                }
77
78
                $paths = $this->patchFileAnalyser->getAllPaths(
79
                    $this->patchFileLoader->loadWithNormalizedLineEndings($path)
80
                );
81
                
82
                $bundleTargets = $this->packageInfoResolver->resolveNamesFromPaths($packagesByName, $paths);
83
                
84
                if (!$bundleTargets && !$this->gracefulMode) {
85
                    throw $this->createError('zero matches', $source);
86
                }
87
88
                $patches[$patchTarget][$index][PatchDefinition::TARGETS] = array_unique($bundleTargets);
89
            }
90
        }
91
92
        return $patches;
93
    }
94
    
95
    private function createError($reason, $source)
96
    {
97
        return new \Vaimo\ComposerPatches\Exceptions\LoaderException(
98
            sprintf('Could not resolve targets (%s): %s ', $reason, $source)
99
        );
100
    }
101
}
102