HideMigrationStorage   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 15
c 1
b 0
f 0
dl 0
loc 34
ccs 15
cts 15
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 11 3
A dispatch() 0 13 5
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\DBAL\EventListener;
6
7
use Doctrine\DBAL\Schema\AbstractAsset;
8
use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand;
9
use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand;
10
use Symfony\Component\Console\Event\ConsoleCommandEvent;
11
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
12
13
/**
14
 * Acts as a console command event dispatcher, and a schema filter, that hides the migration
15
 * metadata table when the command executed is comparing actual DB structure and what it should be.
16
 */
17
final class HideMigrationStorage implements EventDispatcherInterface
18
{
19
    private bool $enabled = false;
20
21 4
    public function __construct(
22
        private readonly ?string $configurationTableName,
23 4
    ) {}
24
25 4
    public function __invoke(AbstractAsset|string $asset): bool
26
    {
27 4
        if (!$this->enabled) {
28 2
            return true;
29
        }
30
31 2
        if ($asset instanceof AbstractAsset) {
32 2
            $asset = $asset->getName();
33
        }
34
35 2
        return $asset !== $this->configurationTableName;
36
    }
37
38 3
    public function dispatch(object $event, ?string $eventName = null): object
39
    {
40
        if (
41 3
            $this->configurationTableName
42 3
            && $event instanceof ConsoleCommandEvent
43
            && (
44 3
                $event->getCommand() instanceof ValidateSchemaCommand
45 3
                || $event->getCommand() instanceof UpdateCommand
46
            )) {
47 2
            $this->enabled = true;
48
        }
49
50 3
        return $event;
51
    }
52
}
53