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\Console\Command; |
20
|
|
|
|
21
|
|
|
use Surfnet\StepupMiddleware\MiddlewareBundle\EventSourcing\EventCollection; |
22
|
|
|
use Surfnet\StepupMiddleware\MiddlewareBundle\EventSourcing\ProjectorCollection; |
23
|
|
|
use Surfnet\StepupMiddleware\MiddlewareBundle\Service\PastEventsService; |
|
|
|
|
24
|
|
|
use Surfnet\StepupMiddleware\MiddlewareBundle\Service\TransactionAwareEventDispatcher; |
|
|
|
|
25
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
26
|
|
|
use Symfony\Component\Console\Command\Command; |
27
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
28
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
29
|
|
|
use Symfony\Component\Console\Input\InputOption; |
30
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
31
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
32
|
|
|
|
33
|
|
|
#[AsCommand( |
34
|
|
|
name: 'stepup:event:replay', |
35
|
|
|
description: 'replay specified events for specified projectors' |
36
|
|
|
)] |
37
|
|
|
class ReplaySpecificEventsCommand extends Command |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
public const OPTION_LIST_EVENTS = 'list-events'; |
40
|
|
|
public const OPTION_LIST_PROJECTORS = 'list-projectors'; |
41
|
|
|
|
42
|
|
|
protected function configure(): void |
43
|
|
|
{ |
44
|
|
|
$this |
45
|
|
|
->addOption( |
46
|
|
|
self::OPTION_LIST_EVENTS, |
47
|
|
|
null, |
48
|
|
|
InputOption::VALUE_NONE, |
49
|
|
|
'List all events available to replay', |
50
|
|
|
) |
51
|
|
|
->addOption( |
52
|
|
|
self::OPTION_LIST_PROJECTORS, |
53
|
|
|
null, |
54
|
|
|
InputOption::VALUE_NONE, |
55
|
|
|
'List all projectors available for which events can be replayed', |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function __construct( |
60
|
|
|
private readonly EventCollection $collection, |
61
|
|
|
private readonly ProjectorCollection $projectorCollection, |
62
|
|
|
private readonly PastEventsService $pastEventsService, |
63
|
|
|
private readonly TransactionAwareEventDispatcher $eventDispatcher, |
64
|
|
|
) { |
65
|
|
|
parent::__construct(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
69
|
|
|
{ |
70
|
|
|
$availableEvents = $this->collection->getEventNames(); |
71
|
|
|
$availableProjectors = $this->projectorCollection->getProjectorNames(); |
72
|
|
|
|
73
|
|
|
if ($input->getOption(self::OPTION_LIST_EVENTS)) { |
74
|
|
|
$output->writeln('<info>The following events can be replayed:</info>'); |
75
|
|
|
$output->writeln($availableEvents === [] ? 'None.' : $availableEvents); |
76
|
|
|
|
77
|
|
|
return 0; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($input->getOption(self::OPTION_LIST_PROJECTORS)) { |
81
|
|
|
$output->writeln('<info>Events can be replayed for the following projectors:</info>'); |
82
|
|
|
$output->writeln($availableProjectors === [] ? 'None.' : $availableProjectors); |
83
|
|
|
|
84
|
|
|
return 0; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($availableProjectors === []) { |
88
|
|
|
$output->writeln('<error>There are no projectors configured to replay events for</error>'); |
89
|
|
|
|
90
|
|
|
return 1; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** @var QuestionHelper $questionHelper */ |
|
|
|
|
94
|
|
|
$questionHelper = $this->getHelper('question'); |
95
|
|
|
|
96
|
|
|
$selectEventsQuestion = new ChoiceQuestion( |
97
|
|
|
'Which events would you like to replay? Please supply a comma-separated list of numbers.', |
98
|
|
|
$availableEvents, |
99
|
|
|
); |
100
|
|
|
$selectEventsQuestion->setMultiselect(true); |
101
|
|
|
|
102
|
|
|
$chosenEvents = $questionHelper->ask($input, $output, $selectEventsQuestion); |
103
|
|
|
$eventSelection = $this->collection->select($chosenEvents); |
104
|
|
|
|
105
|
|
|
$selectProjectorsQuestion = new ChoiceQuestion( |
106
|
|
|
'For which projectors would you like to replay the selected events? ' |
107
|
|
|
. 'Please supply a comma-separated list of numbers.', |
108
|
|
|
$availableProjectors, |
109
|
|
|
); |
110
|
|
|
$selectProjectorsQuestion->setMultiselect(true); |
111
|
|
|
|
112
|
|
|
$chosenProjectors = $questionHelper->ask($input, $output, $selectProjectorsQuestion); |
113
|
|
|
$projectorSelection = $this->projectorCollection->selectByNames($chosenProjectors); |
114
|
|
|
|
115
|
|
|
$events = $this->pastEventsService->findEventsBy($eventSelection); |
116
|
|
|
|
117
|
|
|
$output->writeln('<info>Registering projectors</info>'); |
118
|
|
|
foreach ($projectorSelection as $projector) { |
119
|
|
|
$this->eventDispatcher->registerProjector($projector); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$output->writeln('<info>Dispatching events</info>'); |
123
|
|
|
$this->eventDispatcher->dispatch($events); |
124
|
|
|
return 0; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|