-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathDbalCleanupTaskHandler.php
More file actions
59 lines (44 loc) · 1.7 KB
/
Copy pathDbalCleanupTaskHandler.php
File metadata and controls
59 lines (44 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcing\Subscription\Cleanup\Dbal;
use Doctrine\DBAL\Connection;
use Doctrine\Persistence\ConnectionRegistry;
use Patchlevel\EventSourcing\Subscription\Cleanup\CleanupTaskHandler;
use Patchlevel\EventSourcing\Subscription\Cleanup\CleanupTaskNotSupported;
final class DbalCleanupTaskHandler implements CleanupTaskHandler
{
public function __construct(
private readonly Connection|ConnectionRegistry $connection,
) {
}
public function __invoke(object $task): void
{
if ($task instanceof DropTableTask) {
$this->connection($task->connectionName)->createSchemaManager()->dropTable($task->table);
return;
}
if ($task instanceof DropIndexTask) {
$this->connection($task->connectionName)->createSchemaManager()->dropIndex($task->index, $task->table);
return;
}
throw new CleanupTaskNotSupported($task, self::class);
}
public function supports(object $task): bool
{
return $task instanceof DropTableTask || $task instanceof DropIndexTask;
}
private function connection(string|null $connectionName): Connection
{
if ($this->connection instanceof ConnectionRegistry) {
$connection = $this->connection->getConnection($connectionName);
if (!$connection instanceof Connection) {
throw new UnexpectedConnectionType($connectionName, Connection::class, $connection::class);
}
return $connection;
}
if ($connectionName === null) {
return $this->connection;
}
throw new ConnectionNameNotSupported($connectionName);
}
}