Completed
Pull Request — develop (#185)
by A.
05:01
created

ReplaySpecificEventsCommand::execute()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 41
rs 8.439
cc 5
eloc 24
nc 3
nop 2
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 Broadway\ReadModel\ProjectorInterface;
22
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
23
use Symfony\Component\Console\Helper\QuestionHelper;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Input\InputOption;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Symfony\Component\Console\Question\ChoiceQuestion;
28
29
class ReplaySpecificEventsCommand extends ContainerAwareCommand
30
{
31
    const OPTION_LIST_EVENTS = 'list-events';
32
    const OPTION_LIST_PROJECTORS = 'list-projectors';
33
34
    protected function configure()
35
    {
36
        $this
37
            ->setName('stepup:event:replay')
38
            ->setDescription('replay specified events for specified projectors')
39
            ->addOption(
40
                self::OPTION_LIST_EVENTS,
41
                null,
42
                InputOption::VALUE_NONE,
43
                'List all events available to replay'
44
            )
45
            ->addOption(
46
                self::OPTION_LIST_PROJECTORS,
47
                null,
48
                InputOption::VALUE_NONE,
49
                'List all projectors available for which events can be replayed'
50
            );
51
    }
52
53
    public function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $container = $this->getContainer();
56
        $eventCollection     = $container->get('middleware.event_replay.event_collection');
57
        $projectorCollection = $container->get('middleware.event_replay.projector_collection');
58
59
        if ($input->getOption(self::OPTION_LIST_EVENTS)) {
60
            $availableEvents = iterator_to_array($eventCollection);
61
62
            $output->writeln('<info>The following events can be replayed:</info>');
63
            $output->writeln(!empty($availableEvents) ? $availableEvents : 'None.');
64
65
            return;
66
        }
67
68
        if ($input->getOption(self::OPTION_LIST_PROJECTORS)) {
69
            $availableProjectors = array_map(
70
                function (ProjectorInterface $projector) {
71
                    return get_class($projector);
72
                },
73
                iterator_to_array($projectorCollection)
74
            );
75
76
            $output->writeln('<info>Events can be replayed for the following projectors:</info>');
77
            $output->writeln(!empty($availableProjectors) ? $availableProjectors : 'None.');
78
79
            return;
80
        }
81
82
        /** @var QuestionHelper $questionHelper */
83
        $questionHelper = $this->getHelper('question');
84
85
        $selectEventsQuestion = new ChoiceQuestion(
86
            'Which events would you like to replay? Please supply a comma-separated list of numbers.',
87
            iterator_to_array($eventCollection)
88
        );
89
        $selectEventsQuestion->setMultiselect(true);
90
91
        $chosenEvents = $questionHelper->ask($input, $output, $selectEventsQuestion);
92
        $eventCollection->select($chosenEvents);
93
    }
94
}
95