HideMigrationStorage::dispatch()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.6111
cc 5
nc 2
nop 2
crap 5
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