Completed
Push — master ( ef5d35...9af83e )
by Thomas
12s queued 10s
created

ManageConfigurationPlugin::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the ekino Drupal Debug project.
7
 *
8
 * (c) ekino
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\Drupal\Debug\Composer\Plugin;
15
16
use Composer\Composer;
17
use Composer\DependencyResolver\Operation\UninstallOperation;
18
use Composer\DependencyResolver\Operation\UpdateOperation;
19
use Composer\EventDispatcher\EventSubscriberInterface;
20
use Composer\Installer\PackageEvent;
21
use Composer\Installer\PackageEvents;
22
use Composer\IO\IOInterface;
23
use Composer\Package\PackageInterface;
24
use Composer\Plugin\PluginInterface;
25
use Ekino\Drupal\Debug\Composer\Helper\ManageConfigurationHelper;
26
27
class ManageConfigurationPlugin implements PluginInterface, EventSubscriberInterface
28
{
29
    /**
30
     * @var string
31
     */
32
    const THIS_PACKAGE_NAME = 'ekino/drupal-debug';
33
34
    /**
35
     * @var ManageConfigurationHelper
36
     */
37
    private $manageConfigurationHelper;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 1
    public function activate(Composer $composer, IOInterface $io): void
43
    {
44 1
        $this->manageConfigurationHelper = new ManageConfigurationHelper($composer, $io);
45 1
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public static function getSubscribedEvents(): array
51
    {
52
        return array(
53 1
            PackageEvents::POST_PACKAGE_UPDATE => 'onPostPackageUpdate',
54 1
            PackageEvents::PRE_PACKAGE_UNINSTALL => 'onPrePackageUninstall',
55
        );
56
    }
57
58
    /**
59
     * @param PackageEvent $event
60
     */
61 6
    public function onPostPackageUpdate(PackageEvent $event): void
62
    {
63 6
        if ($this->shouldProcess($event)) {
64 2
            $this->manageConfigurationHelper->warnAboutPotentialConfigurationChanges();
65
        }
66 6
    }
67
68
    /**
69
     * @param PackageEvent $event
70
     */
71 6
    public function onPrePackageUninstall(PackageEvent $event): void
72
    {
73 6
        if ($this->shouldProcess($event)) {
74 2
            $this->manageConfigurationHelper->askForConfigurationFileDeletion();
75
        }
76 6
    }
77
78
    /**
79
     * @param PackageEvent $event
80
     *
81
     * @return bool
82
     */
83 12
    private function shouldProcess(PackageEvent $event): bool
84
    {
85 12
        if ($event->isPropagationStopped()) {
86 2
            return false;
87
        }
88
89 10
        $operation = $event->getOperation();
90 10
        switch (\get_class($operation)) {
91
            case UpdateOperation::class:
92
                /** @var UpdateOperation $operation */
93 4
                $package = $operation->getTargetPackage();
94
95 4
                break;
96
            case UninstallOperation::class:
97
                /** @var UninstallOperation $operation */
98 4
                $package = $operation->getPackage();
99
100 4
                break;
101
            default:
102 2
                return false;
103
        }
104
105
        /* @var PackageInterface $package  */
106 8
        return self::THIS_PACKAGE_NAME === $package->getName();
107
    }
108
}
109