-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProfileProjection.php
More file actions
79 lines (65 loc) · 2.07 KB
/
Copy pathProfileProjection.php
File metadata and controls
79 lines (65 loc) · 2.07 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcing\Tests\Integration\Subscription\Subscriber;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Table;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\Subscription\Subscriber\BatchableSubscriber;
use Patchlevel\EventSourcing\Tests\Integration\Subscription\Events\ProfileCreated;
#[Projector(self::SUBSCRIBER_ID)]
final class ProfileProjection implements BatchableSubscriber
{
private const SUBSCRIBER_ID = 'profile_1';
public function __construct(
private Connection $connection,
) {
}
#[Setup]
public function create(): void
{
$table = new Table($this->tableName());
$table->addColumn('id', 'string')->setLength(36);
$table->addColumn('name', 'string')->setLength(255);
$table->setPrimaryKey(['id']);
$this->connection->createSchemaManager()->createTable($table);
}
#[Teardown]
public function drop(): void
{
$this->connection->createSchemaManager()->dropTable($this->tableName());
}
#[Subscribe(ProfileCreated::class)]
public function handleProfileCreated(ProfileCreated $profileCreated): void
{
$this->connection->executeStatement(
'INSERT INTO ' . $this->tableName() . ' (id, name) VALUES(:id, :name);',
[
'id' => $profileCreated->profileId->toString(),
'name' => $profileCreated->name,
],
);
}
private function tableName(): string
{
return 'projection_' . self::SUBSCRIBER_ID;
}
public function beginBatch(): void
{
$this->connection->beginTransaction();
}
public function commitBatch(): void
{
$this->connection->commit();
}
public function rollbackBatch(): void
{
$this->connection->rollBack();
}
public function forceCommit(): bool
{
return false;
}
}