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

ItemProcessor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 27
dl 0
loc 78
rs 10
c 1
b 0
f 1
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processFileInfo() 0 23 3
A dispatchEventForPackagePatch() 0 5 1
A __construct() 0 12 1
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Package\PatchApplier;
7
8
use Composer\Package\PackageInterface;
9
10
use Vaimo\ComposerPatches\Patch\Event;
11
use Vaimo\ComposerPatches\Events;
12
use Vaimo\ComposerPatches\Patch\Definition as Patch;
13
14
class ItemProcessor
15
{
16
    /**
17
     * @var \Composer\EventDispatcher\EventDispatcher
18
     */
19
    private $eventDispatcher;
20
21
    /**
22
     * @var \Vaimo\ComposerPatches\Package\InfoResolver
23
     */
24
    private $packageInfoResolver;
25
26
    /**
27
     * @var \Vaimo\ComposerPatches\Interfaces\PatchFailureHandlerInterface
28
     */
29
    private $failureHandler;
30
31
    /**
32
     * @var \Vaimo\ComposerPatches\Patch\File\Applier
33
     */
34
    private $fileApplier;
35
36
    /**
37
     * @var \Vaimo\ComposerPatches\Logger
38
     */
39
    private $logger;
40
41
    /**
42
     * @param \Composer\EventDispatcher\EventDispatcher $eventDispatcher
43
     * @param \Vaimo\ComposerPatches\Package\InfoResolver $packageInfoResolver
44
     * @param \Vaimo\ComposerPatches\Interfaces\PatchFailureHandlerInterface $failureHandler
45
     * @param \Vaimo\ComposerPatches\Patch\File\Applier $fileApplier
46
     * @param \Vaimo\ComposerPatches\Logger $logger
47
     */
48
    public function __construct(
49
        \Composer\EventDispatcher\EventDispatcher $eventDispatcher,
50
        \Vaimo\ComposerPatches\Package\InfoResolver $packageInfoResolver,
51
        \Vaimo\ComposerPatches\Interfaces\PatchFailureHandlerInterface $failureHandler,
52
        \Vaimo\ComposerPatches\Patch\File\Applier $fileApplier,
53
        \Vaimo\ComposerPatches\Logger $logger
54
    ) {
55
        $this->eventDispatcher = $eventDispatcher;
56
        $this->packageInfoResolver = $packageInfoResolver;
57
        $this->failureHandler = $failureHandler;
58
        $this->fileApplier = $fileApplier;
59
        $this->logger = $logger;
60
    }
61
    
62
    public function processFileInfo(PackageInterface $package, array $info)
63
    {
64
        $source = $info[Patch::SOURCE];
65
66
        try {
67
            $installPath = $this->packageInfoResolver->getInstallPath($package, $info[Patch::CWD]);
68
69
            $this->dispatchEventForPackagePatch(Events::PRE_APPLY, $package, $info);
70
71
            $this->fileApplier->applyFile($info[Patch::PATH], $installPath, $info[Patch::CONFIG]);
72
73
            $this->dispatchEventForPackagePatch(Events::POST_APPLY, $package, $info);
74
75
            return true;
76
        } catch (\Vaimo\ComposerPatches\Exceptions\RuntimeException $runtimeException) {
77
            throw $runtimeException;
78
        } catch (\Exception $exception) {
79
            $this->logger->writeException($exception);
80
81
            $this->failureHandler->execute($exception, $source);
82
        }
83
84
        return false;
85
    }
86
87
    private function dispatchEventForPackagePatch($name, PackageInterface $package, array $patch)
88
    {
89
        $this->eventDispatcher->dispatch(
90
            Events::PRE_APPLY,
91
            new Event($name, $package, $patch[Patch::SOURCE], $patch[Patch::LABEL])
92
        );
93
    }
94
}
95