ReplaySpecificEventsCommand::execute()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 34
nc 5
nop 2
dl 0
loc 57
rs 8.4426
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
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\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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ReplaySpecificEventsCommand
Loading history...
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 */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
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