ManageConfigurationPlugin   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 24
c 2
b 1
f 1
dl 0
loc 80
ccs 25
cts 25
cp 1
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A onPrePackageUninstall() 0 4 2
A shouldProcess() 0 24 4
A getSubscribedEvents() 0 5 1
A activate() 0 3 1
A onPostPackageUpdate() 0 4 2
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