-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEventDrivenCommandGenerator.php
More file actions
118 lines (90 loc) · 3.55 KB
/
EventDrivenCommandGenerator.php
File metadata and controls
118 lines (90 loc) · 3.55 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
namespace Cycle\ORM\Entity\Behavior;
use Cycle\ORM\Command\CommandInterface;
use Cycle\ORM\Entity\Behavior\Dispatcher\Dispatcher;
use Cycle\ORM\Entity\Behavior\Dispatcher\ListenerProvider;
use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnCreate;
use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnDelete;
use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnUpdate;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\SchemaInterface;
use Cycle\ORM\Transaction\CommandGenerator;
use Cycle\ORM\Transaction\Tuple;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
final class EventDrivenCommandGenerator extends CommandGenerator
{
private \DateTimeImmutable $timestamp;
public function __construct(
SchemaInterface $schema,
ContainerInterface $container,
private ?EventDispatcherInterface $eventDispatcher = null
) {
if ($eventDispatcher === null) {
$listenerProvider = new ListenerProvider($schema, $container);
$this->eventDispatcher = new Dispatcher($listenerProvider);
}
}
protected function storeEntity(ORMInterface $orm, Tuple $tuple, bool $isNew): ?CommandInterface
{
$role = $tuple->node->getRole();
$src = $orm->getSource($role);
$event = $isNew
? new OnCreate($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $src, $this->timestamp)
: new OnUpdate($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $src, $this->timestamp);
$event->command = parent::storeEntity($orm, $tuple, $isNew);
$event = $this->eventDispatcher->dispatch($event);
return $event->command;
}
/**
* @param non-empty-string $parentRole
*/
protected function generateParentStoreCommand(
ORMInterface $orm,
Tuple $tuple,
string $parentRole,
bool $isNew
): ?CommandInterface {
$mapper = $orm->getMapper($parentRole);
$source = $orm->getSource($parentRole);
$event =
new OnCreate($parentRole, $mapper, $tuple->entity, $tuple->node, $tuple->state, $source, $this->timestamp);
$event->command = $isNew
? $mapper->queueCreate($tuple->entity, $tuple->node, $tuple->state)
: $mapper->queueUpdate($tuple->entity, $tuple->node, $tuple->state);
$event = $this->eventDispatcher->dispatch($event);
return $event->command;
}
protected function deleteEntity(ORMInterface $orm, Tuple $tuple): ?CommandInterface
{
$role = $tuple->node->getRole();
$source = $orm->getSource($role);
$event = new OnDelete(
$role,
$tuple->mapper,
$tuple->entity,
$tuple->node,
$tuple->state,
$source,
$this->timestamp
);
$event->command = parent::deleteEntity($orm, $tuple);
$event = $this->eventDispatcher->dispatch($event);
return $event->command;
}
public function generateStoreCommand(ORMInterface $orm, Tuple $tuple): ?CommandInterface
{
$this->timestamp = new \DateTimeImmutable();
$command = parent::generateStoreCommand($orm, $tuple);
unset($this->timestamp);
return $command;
}
public function generateDeleteCommand(ORMInterface $orm, Tuple $tuple): ?CommandInterface
{
$this->timestamp = new \DateTimeImmutable();
$command = parent::generateDeleteCommand($orm, $tuple);
unset($this->timestamp);
return $command;
}
}