Passed
Push — master ( b12c89...119ef1 )
by Allan
02:13
created

PatchApplier::dispatchEventForPackagePatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 10
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\Package;
7
8
use Composer\Package\PackageInterface;
9
10
use Vaimo\ComposerPatches\Patch\Definition as Patch;
11
12
class PatchApplier
13
{
14
    /**
15
     * @var \Vaimo\ComposerPatches\Package\PatchApplier\ItemProcessor
16
     */
17
    private $fileProcessor;
18
    
19
    /**
20
     * @var \Vaimo\ComposerPatches\Package\PatchApplier\InfoLogger
21
     */
22
    private $detailsLogger;
23
24
    /**
25
     * @var \Vaimo\ComposerPatches\Strategies\OutputStrategy
26
     */
27
    private $outputStrategy;
28
    
29
    /**
30
     * @var \Vaimo\ComposerPatches\Logger
31
     */
32
    private $logger;
33
34
    /**
35
     * @param \Vaimo\ComposerPatches\Package\PatchApplier\ItemProcessor $fileProcessor
36
     * @param \Vaimo\ComposerPatches\Package\PatchApplier\InfoLogger $detailsLogger
37
     * @param \Vaimo\ComposerPatches\Strategies\OutputStrategy $outputStrategy
38
     * @param \Vaimo\ComposerPatches\Logger $logger
39
     */
40
    public function __construct(
41
        \Vaimo\ComposerPatches\Package\PatchApplier\ItemProcessor $fileProcessor,
42
        \Vaimo\ComposerPatches\Package\PatchApplier\InfoLogger $detailsLogger,
43
        \Vaimo\ComposerPatches\Strategies\OutputStrategy $outputStrategy,
44
        \Vaimo\ComposerPatches\Logger $logger
45
    ) {
46
        $this->fileProcessor = $fileProcessor;
47
        $this->detailsLogger = $detailsLogger;
48
        $this->outputStrategy = $outputStrategy;
49
        $this->logger = $logger;
50
    }
51
    
52
    public function applyPatches(PackageInterface $package, array $patchesQueue)
53
    {
54
        $appliedPatches = array();
55
        
56
        foreach ($patchesQueue as $source => $info) {
57
            $muteDepth = !$this->outputStrategy->shouldAllowForPatches(array($info))
58
                ? $this->logger->mute()
59
                : null;
60
61
            $patchInfo = array_replace($info, array(Patch::SOURCE => $source));
62
            
63
            $this->detailsLogger->outputPatchSource($patchInfo);
64
65
            $loggerIndentation = $this->logger->push();
66
67
            $this->detailsLogger->outputPatchDescription($patchInfo);
68
69
            try {
70
                $result = $this->fileProcessor->processFileInfo($package, $patchInfo);
71
            } catch (\Exception $exception) {
72
                $this->logger->reset($loggerIndentation);
73
                
74
                if ($muteDepth !== null) {
75
                    $this->logger->unMute($muteDepth);
76
                }
77
78
                throw $exception;
79
            }
80
81
            $this->logger->reset($loggerIndentation);
82
            
83
            if ($muteDepth !== null) {
84
                $this->logger->unMute($muteDepth);
85
            }
86
87
            if (!$result) {
88
                continue;
89
            }
90
91
            $appliedPatches[$source] = $info;
92
        }
93
94
        return $appliedPatches;
95
    }
96
}
97