PatchApplier   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B applyPatches() 0 40 7
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
    /**
53
     * @param PackageInterface $package
54
     * @param array $patchesQueue
55
     * @return array
56
     * @throws \Exception
57
     * @throws \Vaimo\ComposerPatches\Exceptions\PatchFailureException
58
     */
59
    public function applyPatches(PackageInterface $package, array $patchesQueue)
60
    {
61
        $appliedPatches = array();
62
63
        foreach ($patchesQueue as $source => $info) {
64
            $muteDepth = !$this->outputStrategy->shouldAllowForPatches(array($info))
65
                ? $this->logger->mute()
66
                : null;
67
68
            $patchInfo = array_replace($info, array(Patch::SOURCE => $source));
69
            $this->detailsLogger->outputPatchSource($patchInfo);
70
            $loggerIndentation = $this->logger->push();
71
            $this->detailsLogger->outputPatchDescription($patchInfo);
72
73
            try {
74
                $result = $this->fileProcessor->processFileInfo($package, $patchInfo);
75
            } catch (\Exception $exception) {
76
                $this->logger->reset($loggerIndentation);
77
78
                if ($muteDepth !== null) {
79
                    $this->logger->unMute($muteDepth);
80
                }
81
82
                throw $exception;
83
            }
84
85
            $this->logger->reset($loggerIndentation);
86
87
            if ($muteDepth !== null) {
88
                $this->logger->unMute($muteDepth);
89
            }
90
91
            if (!$result) {
92
                continue;
93
            }
94
95
            $appliedPatches[$source] = $info;
96
        }
97
98
        return $appliedPatches;
99
    }
100
}
101