1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Cycle\ORM\Entity\Behavior; |
||
6 | |||
7 | use Cycle\ORM\Command\CommandInterface; |
||
8 | use Cycle\ORM\Entity\Behavior\Dispatcher\Dispatcher; |
||
9 | use Cycle\ORM\Entity\Behavior\Dispatcher\ListenerProvider; |
||
10 | use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnCreate; |
||
11 | use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnDelete; |
||
12 | use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnUpdate; |
||
13 | use Cycle\ORM\ORMInterface; |
||
14 | use Cycle\ORM\SchemaInterface; |
||
15 | use Cycle\ORM\Transaction\CommandGenerator; |
||
16 | use Cycle\ORM\Transaction\Tuple; |
||
17 | use Psr\Container\ContainerInterface; |
||
18 | use Psr\EventDispatcher\EventDispatcherInterface; |
||
19 | |||
20 | final class EventDrivenCommandGenerator extends CommandGenerator |
||
21 | { |
||
22 | private EventDispatcherInterface $eventDispatcher; |
||
23 | private \DateTimeImmutable $timestamp; |
||
24 | |||
25 | 120 | public function __construct(SchemaInterface $schema, ContainerInterface $container) |
|
26 | { |
||
27 | 120 | $listenerProvider = new ListenerProvider($schema, $container); |
|
28 | |||
29 | 120 | $this->eventDispatcher = new Dispatcher($listenerProvider); |
|
30 | } |
||
31 | |||
32 | 120 | public function generateStoreCommand(ORMInterface $orm, Tuple $tuple): ?CommandInterface |
|
33 | { |
||
34 | 120 | $this->timestamp = new \DateTimeImmutable(); |
|
35 | 120 | ||
36 | $command = parent::generateStoreCommand($orm, $tuple); |
||
37 | 120 | ||
38 | 120 | unset($this->timestamp); |
|
39 | 56 | ||
40 | return $command; |
||
41 | 120 | } |
|
42 | |||
43 | 120 | public function generateDeleteCommand(ORMInterface $orm, Tuple $tuple): ?CommandInterface |
|
44 | { |
||
45 | 120 | $this->timestamp = new \DateTimeImmutable(); |
|
46 | |||
47 | $command = parent::generateDeleteCommand($orm, $tuple); |
||
48 | |||
49 | unset($this->timestamp); |
||
50 | |||
51 | return $command; |
||
52 | } |
||
53 | |||
54 | protected function storeEntity(ORMInterface $orm, Tuple $tuple, bool $isNew): ?CommandInterface |
||
55 | { |
||
56 | $role = $tuple->node->getRole(); |
||
57 | $src = $orm->getSource($role); |
||
58 | |||
59 | $event = $isNew |
||
60 | ? new OnCreate($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $src, $this->timestamp) |
||
61 | : new OnUpdate($role, $tuple->mapper, $tuple->entity, $tuple->node, $tuple->state, $src, $this->timestamp); |
||
62 | |||
63 | $event->command = parent::storeEntity($orm, $tuple, $isNew); |
||
64 | |||
65 | $event = $this->eventDispatcher->dispatch($event); |
||
66 | |||
67 | return $event->command; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param non-empty-string $parentRole |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
72 | 8 | */ |
|
73 | protected function generateParentStoreCommand( |
||
74 | 8 | ORMInterface $orm, |
|
75 | 8 | Tuple $tuple, |
|
76 | string $parentRole, |
||
77 | 8 | bool $isNew, |
|
78 | ): ?CommandInterface { |
||
79 | 8 | $mapper = $orm->getMapper($parentRole); |
|
80 | 8 | $source = $orm->getSource($parentRole); |
|
81 | 8 | ||
82 | 8 | $event = |
|
83 | new OnCreate($parentRole, $mapper, $tuple->entity, $tuple->node, $tuple->state, $source, $this->timestamp); |
||
84 | 8 | ||
85 | $event->command = $isNew |
||
86 | ? $mapper->queueCreate($tuple->entity, $tuple->node, $tuple->state) |
||
87 | 8 | : $mapper->queueUpdate($tuple->entity, $tuple->node, $tuple->state); |
|
88 | |||
89 | 8 | $event = $this->eventDispatcher->dispatch($event); |
|
90 | |||
91 | 8 | return $event->command; |
|
92 | } |
||
93 | |||
94 | 120 | protected function deleteEntity(ORMInterface $orm, Tuple $tuple): ?CommandInterface |
|
95 | { |
||
96 | 120 | $role = $tuple->node->getRole(); |
|
97 | $source = $orm->getSource($role); |
||
98 | 120 | ||
99 | $event = new OnDelete( |
||
100 | 120 | $role, |
|
101 | $tuple->mapper, |
||
102 | 120 | $tuple->entity, |
|
103 | $tuple->node, |
||
104 | $tuple->state, |
||
105 | 8 | $source, |
|
106 | $this->timestamp, |
||
107 | 8 | ); |
|
108 | |||
109 | 8 | $event->command = parent::deleteEntity($orm, $tuple); |
|
110 | |||
111 | 8 | $event = $this->eventDispatcher->dispatch($event); |
|
112 | |||
113 | 8 | return $event->command; |
|
114 | } |
||
115 | } |
||
116 |