1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2017 SURFnet B.V. |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddleware\MiddlewareBundle\Service; |
20
|
|
|
|
21
|
|
|
use Broadway\Domain\DomainMessage; |
22
|
|
|
use Exception; |
23
|
|
|
use Surfnet\StepupMiddleware\MiddlewareBundle\EventSourcing\DBALEventHydrator; |
24
|
|
|
use Surfnet\StepupMiddleware\MiddlewareBundle\EventSourcing\EventCollection; |
25
|
|
|
use Surfnet\StepupMiddleware\MiddlewareBundle\EventSourcing\ProjectorCollection; |
26
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
27
|
|
|
|
28
|
|
|
final class SpecificEventDispatcher |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var DBALConnectionHelper |
32
|
|
|
*/ |
33
|
|
|
private $connectionHelper; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var DBALEventHydrator |
37
|
|
|
*/ |
38
|
|
|
private $eventHydrator; |
39
|
|
|
|
40
|
|
|
public function __construct(DBALConnectionHelper $connectionHelper, DBALEventHydrator $eventHydrator) |
41
|
|
|
{ |
42
|
|
|
$this->connectionHelper = $connectionHelper; |
43
|
|
|
$this->eventHydrator = $eventHydrator; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function dispatchEventsForProjectors( |
47
|
|
|
EventCollection $eventCollection, |
48
|
|
|
ProjectorCollection $projectorCollection, |
49
|
|
|
OutputInterface $output |
50
|
|
|
) { |
51
|
|
|
$output->writeln('<info>Starting event dispatch</info>'); |
52
|
|
|
$this->connectionHelper->beginTransaction(); |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
$output->writeln('<info>Hydrating selected events</info>'); |
56
|
|
|
$events = $this->eventHydrator->getEventsFrom($eventCollection); |
57
|
|
|
|
58
|
|
|
$output->writeln('<info>Attempting to handle selected events with selected projectors:</info>'); |
59
|
|
|
|
60
|
|
|
/** @var DomainMessage $event */ |
61
|
|
|
foreach ($events as $event) { |
62
|
|
|
$output->writeln(sprintf(' <info>> Event</info> "%s"', $event->getType())); |
63
|
|
|
|
64
|
|
|
foreach ($projectorCollection as $projectorName => $projector) { |
65
|
|
|
$output->writeln(sprintf(' <info>> Projector</info> "%s"', $projectorName)); |
66
|
|
|
$projector->handle($event); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$output->writeln('Event dispatch successful'); |
71
|
|
|
$this->connectionHelper->commit(); |
72
|
|
|
} catch (Exception $exception) { |
73
|
|
|
$output->writeln(sprintf('<error>Event dispatch failed: %s</error>', $exception->getMessage())); |
74
|
|
|
$this->connectionHelper->rollBack(); |
75
|
|
|
|
76
|
|
|
throw $exception; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|