Passed
Pull Request — main (#557)
by Johan
10:46 queued 05:30
created

ReplaySpecificEventsCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type Surfnet\StepupMiddleware...rvice\PastEventsService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Surfnet\StepupMiddleware\MiddlewareBundle\Service\TransactionAwareEventDispatcher;
0 ignored issues
show
Bug introduced by
The type Surfnet\StepupMiddleware...ionAwareEventDispatcher was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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