Completed
Push — release-2.x ( 370844...c278f6 )
by A.
05:18
created

ReplaySpecificEventsCommand::execute()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 64
Code Lines 39

Duplication

Lines 12
Ratio 18.75 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
c 9
b 0
f 0
dl 12
loc 64
rs 7.2058
cc 7
eloc 39
nc 5
nop 2

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
 */
18
19
namespace Surfnet\StepupMiddleware\MiddlewareBundle\Console\Command;
20
21
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
22
use Symfony\Component\Console\Helper\QuestionHelper;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Symfony\Component\Console\Question\ChoiceQuestion;
27
28
class ReplaySpecificEventsCommand extends ContainerAwareCommand
29
{
30
    const OPTION_LIST_EVENTS = 'list-events';
31
    const OPTION_LIST_PROJECTORS = 'list-projectors';
32
33
    protected function configure()
34
    {
35
        $this
36
            ->setName('stepup:event:replay')
37
            ->setDescription('replay specified events for specified projectors')
38
            ->addOption(
39
                self::OPTION_LIST_EVENTS,
40
                null,
41
                InputOption::VALUE_NONE,
42
                'List all events available to replay'
43
            )
44
            ->addOption(
45
                self::OPTION_LIST_PROJECTORS,
46
                null,
47
                InputOption::VALUE_NONE,
48
                'List all projectors available for which events can be replayed'
49
            );
50
    }
51
52
    public function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $container = $this->getContainer();
55
        $eventCollection     = $container->get('middleware.event_replay.event_collection');
56
        $projectorCollection = $container->get('middleware.event_replay.projector_collection');
57
58
        $availableEvents     = $eventCollection->getEventNames();
59
        $availableProjectors = $projectorCollection->getProjectorNames();
60
61 View Code Duplication
        if ($input->getOption(self::OPTION_LIST_EVENTS)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
            $output->writeln('<info>The following events can be replayed:</info>');
63
            $output->writeln(!empty($availableEvents) ? $availableEvents : 'None.');
64
65
            return;
66
        }
67
68 View Code Duplication
        if ($input->getOption(self::OPTION_LIST_PROJECTORS)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            $output->writeln('<info>Events can be replayed for the following projectors:</info>');
70
            $output->writeln(!empty($availableProjectors) ? $availableProjectors : 'None.');
71
72
            return;
73
        }
74
75
        if (count($availableProjectors) === 0) {
76
            $output->writeln('<error>There are no projectors configured to reply events for</error>');
77
78
            return;
79
        }
80
81
        /** @var QuestionHelper $questionHelper */
82
        $questionHelper = $this->getHelper('question');
83
84
        $selectEventsQuestion = new ChoiceQuestion(
85
            'Which events would you like to replay? Please supply a comma-separated list of numbers.',
86
            $availableEvents
87
        );
88
        $selectEventsQuestion->setMultiselect(true);
89
90
        $chosenEvents   = $questionHelper->ask($input, $output, $selectEventsQuestion);
91
        $eventSelection = $eventCollection->select($chosenEvents);
92
93
        $selectProjectorsQuestion = new ChoiceQuestion(
94
            'For which projectors would you like to replay the selected events? '
95
            . 'Please supply a comma-separated list of numbers.',
96
            $availableProjectors
97
        );
98
        $selectProjectorsQuestion->setMultiselect(true);
99
100
        $chosenProjectors   = $questionHelper->ask($input, $output, $selectProjectorsQuestion);
101
        $projectorSelection = $projectorCollection->selectByNames($chosenProjectors);
102
103
        $pastEventsService = $container->get('middleware.event_replay.past_events_service');
104
        $events = $pastEventsService->findEventsBy($eventSelection);
105
106
        $eventReplayer = $container->get('middleware.event_replay.transaction_aware_event_dispatcher');
107
108
        $output->writeln('<info>Registering projectors</info>');
109
        foreach ($projectorSelection as $projector) {
110
            $eventReplayer->registerProjector($projector);
111
        }
112
113
        $output->writeln('<info>Dispatching events</info>');
114
        $eventReplayer->dispatch($events);
115
    }
116
}
117