Plugin::activate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\ComposerPackages;
6
7
use Composer\Composer;
8
use Composer\EventDispatcher\EventSubscriberInterface;
9
use Composer\IO\IOInterface;
10
use Composer\Plugin\PluginInterface;
11
use Composer\Script\Event;
12
use Composer\Script\ScriptEvents;
13
use ReflectionException;
14
15
final class Plugin implements EventSubscriberInterface, PluginInterface
16
{
17
    public function activate(Composer $composer, IOInterface $io)
18
    {
19
    }
20
21
    public function deactivate(Composer $composer, IOInterface $io)
22
    {
23
    }
24
25
    /**
26
     * @return array<string, string>
27
     */
28
    public static function getSubscribedEvents(): array
29
    {
30 1
        return [ScriptEvents::POST_AUTOLOAD_DUMP => 'regeneration'];
31
    }
32 1
33
    /**
34
     * @throws ReflectionException
35
     */
36
    public static function regeneration(Event $composerEvent): void
37
    {
38
        // This is to prevent issue when removing the package with composer.
39
        if (false === class_exists(ClassGenerator::class)) {
40 2
            return;
41
        }
42
43 2
        $composerEvent->getIO()->write('<info>drupol/composer-packages:</info> Regenerating classes...');
44
45
        (new ClassGenerator($composerEvent))->regenerateClasses();
46
47 2
        $composerEvent->getIO()->write('<info>drupol/composer-packages:</info> Done.');
48
    }
49 2
50
    public function uninstall(Composer $composer, IOInterface $io)
51 2
    {
52 2
        $files = glob(
53
            sprintf(
54
                '%s/../build/',
55
                __DIR__
56
            )
57
        );
58
59
        if (false === $files) {
60
            return;
61
        }
62
63
        foreach ($files as $file) {
64
            unlink($file);
65
        }
66
    }
67
}
68