1 | <?php |
||
23 | class DefaultCommandTesterWithExplanation implements Command\CommandTesterWithExplanation |
||
24 | { |
||
25 | /** |
||
26 | * @var CommandSubscriber |
||
27 | */ |
||
28 | private $commandSubscriber; |
||
29 | /** |
||
30 | * @var CommandApplier |
||
31 | */ |
||
32 | private $commandApplier; |
||
33 | /** |
||
34 | * @var AggregateRepository |
||
35 | */ |
||
36 | private $aggregateRepository; |
||
37 | /** |
||
38 | * @var EventsApplierOnAggregate |
||
39 | */ |
||
40 | private $eventsApplierOnAggregate; |
||
41 | /** |
||
42 | * @var EventMetadataFactory |
||
43 | */ |
||
44 | private $eventMetadataFactory; |
||
45 | /** |
||
46 | * @var CommandMetadataFactory |
||
47 | */ |
||
48 | private $commandMetadataFactory; |
||
49 | |||
50 | 2 | public function __construct( |
|
51 | CommandSubscriber $commandSubscriber, |
||
52 | CommandApplier $commandApplier, |
||
53 | AggregateRepository $aggregateRepository, |
||
54 | EventsApplierOnAggregate $eventsApplier, |
||
55 | EventMetadataFactory $eventMetadataFactory, |
||
56 | CommandMetadataFactory $commandMetadataFactory |
||
57 | ) |
||
58 | { |
||
59 | 2 | $this->commandSubscriber = $commandSubscriber; |
|
60 | 2 | $this->commandApplier = $commandApplier; |
|
61 | 2 | $this->aggregateRepository = $aggregateRepository; |
|
62 | 2 | $this->eventsApplierOnAggregate = $eventsApplier; |
|
63 | 2 | $this->eventMetadataFactory = $eventMetadataFactory; |
|
64 | 2 | $this->commandMetadataFactory = $commandMetadataFactory; |
|
65 | 2 | } |
|
66 | |||
67 | 2 | public function whyCantExecuteCommand(Command $command) |
|
77 | |||
78 | 2 | private function loadCommandHandlerAndAggregate(CommandWithMetadata $command): CommandHandlerAndAggregate |
|
79 | { |
||
80 | 2 | return new CommandHandlerAndAggregate( |
|
81 | 2 | $this->commandSubscriber->getHandlerForCommand($command->getCommand()), |
|
82 | 2 | $this->aggregateRepository->loadAggregate($this->commandSubscriber->getHandlerForCommand($command->getCommand())->getHandlerClass(), $command->getAggregateId()) |
|
83 | ); |
||
84 | } |
||
85 | |||
86 | 1 | private function decorateEventWithMetaData($event, MetaData $metaData): EventWithMetaData |
|
90 | |||
91 | /** |
||
92 | * @param CommandWithMetadata $command |
||
93 | * @param CommandHandlerAndAggregate $handlerAndAggregate |
||
94 | * @return void |
||
95 | */ |
||
96 | 2 | private function applyCommand(CommandWithMetadata $command, CommandHandlerAndAggregate $handlerAndAggregate) |
|
97 | { |
||
98 | 2 | $aggregate = $handlerAndAggregate->getAggregate(); |
|
99 | 2 | $handler = $handlerAndAggregate->getCommandHandler(); |
|
100 | |||
101 | 2 | $metaData = $this->eventMetadataFactory->factoryEventMetadata($command, $aggregate); |
|
102 | |||
103 | 2 | $newMessageGenerator = $this->commandApplier->applyCommand($aggregate, $command->getCommand(), $handler->getMethodName()); |
|
104 | |||
105 | 2 | foreach ($newMessageGenerator as $message) { |
|
106 | 1 | if (!$this->isScheduledCommand($message)) { |
|
107 | 1 | $eventWithMetaData = $this->decorateEventWithMetaData($message, $metaData); |
|
108 | 1 | if (!$this->isScheduledEvent($message)) { |
|
109 | 1 | $this->eventsApplierOnAggregate->applyEventsOnAggregate($aggregate, [$eventWithMetaData]); |
|
110 | } |
||
111 | } |
||
112 | } |
||
113 | 1 | } |
|
114 | |||
115 | 1 | private function isScheduledEvent($event): bool |
|
119 | |||
120 | 1 | private function isScheduledCommand($message): bool |
|
124 | } |