Plugin::regeneration()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
ccs 3
cts 4
cp 0.75
crap 2.0625
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