Completed
Pull Request — develop (#313)
by Michiel
02:22
created

ReplaySpecificEventsCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 4
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\EventStreamReplayer;
24
use Surfnet\StepupMiddleware\MiddlewareBundle\Service\PastEventsService;
25
use Surfnet\StepupMiddleware\MiddlewareBundle\Service\TransactionAwareEventDispatcher;
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
class ReplaySpecificEventsCommand extends Command
34
{
35
    const OPTION_LIST_EVENTS = 'list-events';
36
    const OPTION_LIST_PROJECTORS = 'list-projectors';
37
38
    /**
39
     * @var EventCollection
40
     */
41
    private $collection;
42
43
    /**
44
     * @var EventStreamReplayer
45
     */
46
    private $replayer;
47
48
    /**
49
     * @var PastEventsService
50
     */
51
    private $pastEventsService;
52
53
    /**
54
     * @var TransactionAwareEventDispatcher
55
     */
56
    private $eventDispatcher;
57
    /**
58
     * @var ProjectorCollection
59
     */
60
    private $projectorCollection;
61
62
    protected function configure()
63
    {
64
        $this
65
            ->setName('stepup:event:replay')
66
            ->setDescription('replay specified events for specified projectors')
67
            ->addOption(
68
                self::OPTION_LIST_EVENTS,
69
                null,
70
                InputOption::VALUE_NONE,
71
                'List all events available to replay'
72
            )
73
            ->addOption(
74
                self::OPTION_LIST_PROJECTORS,
75
                null,
76
                InputOption::VALUE_NONE,
77
                'List all projectors available for which events can be replayed'
78
            );
79
    }
80
81
    public function __construct(
82
        EventCollection $collection,
83
        ProjectorCollection $projectorCollection,
84
        PastEventsService $pastEventsService,
85
        TransactionAwareEventDispatcher $eventDispatcher
86
    ) {
87
        $this->collection = $collection;
88
        $this->projectorCollection = $projectorCollection;
89
        $this->pastEventsService = $pastEventsService;
90
        $this->eventDispatcher = $eventDispatcher;
91
        parent::__construct();
92
    }
93
94
    protected function execute(InputInterface $input, OutputInterface $output)
95
    {
96
        $availableEvents     = $this->collection->getEventNames();
97
        $availableProjectors = $this->projectorCollection->getProjectorNames();
98
99 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...
100
            $output->writeln('<info>The following events can be replayed:</info>');
101
            $output->writeln(!empty($availableEvents) ? $availableEvents : 'None.');
0 ignored issues
show
Bug introduced by
It seems like !empty($availableEvents)...ailableEvents : 'None.' can also be of type array<integer,string>; however, Symfony\Component\Consol...putInterface::writeln() does only seem to accept string|object<Symfony\Co...onsole\Output\iterable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
102
103
            return;
104
        }
105
106 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...
107
            $output->writeln('<info>Events can be replayed for the following projectors:</info>');
108
            $output->writeln(!empty($availableProjectors) ? $availableProjectors : 'None.');
0 ignored issues
show
Bug introduced by
It seems like !empty($availableProject...bleProjectors : 'None.' can also be of type array<integer,string>; however, Symfony\Component\Consol...putInterface::writeln() does only seem to accept string|object<Symfony\Co...onsole\Output\iterable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
109
110
            return;
111
        }
112
113
        if (count($availableProjectors) === 0) {
114
            $output->writeln('<error>There are no projectors configured to reply events for</error>');
115
116
            return;
117
        }
118
119
        /** @var QuestionHelper $questionHelper */
120
        $questionHelper = $this->getHelper('question');
121
122
        $selectEventsQuestion = new ChoiceQuestion(
123
            'Which events would you like to replay? Please supply a comma-separated list of numbers.',
124
            $availableEvents
125
        );
126
        $selectEventsQuestion->setMultiselect(true);
127
128
        $chosenEvents   = $questionHelper->ask($input, $output, $selectEventsQuestion);
129
        $eventSelection = $this->collection->select($chosenEvents);
130
131
        $selectProjectorsQuestion = new ChoiceQuestion(
132
            'For which projectors would you like to replay the selected events? '
133
            . 'Please supply a comma-separated list of numbers.',
134
            $availableProjectors
135
        );
136
        $selectProjectorsQuestion->setMultiselect(true);
137
138
        $chosenProjectors   = $questionHelper->ask($input, $output, $selectProjectorsQuestion);
139
        $projectorSelection = $this->projectorCollection->selectByNames($chosenProjectors);
140
141
        $events = $this->pastEventsService->findEventsBy($eventSelection);
142
143
        $output->writeln('<info>Registering projectors</info>');
144
        foreach ($projectorSelection as $projector) {
145
            $this->eventDispatcher->registerProjector($projector);
146
        }
147
148
        $output->writeln('<info>Dispatching events</info>');
149
        $this->eventDispatcher->dispatch($events);
0 ignored issues
show
Documentation introduced by
$events is of type object<Broadway\Domain\DomainEventStream>, but the function expects a object<Broadway\Domain\D...inEventStreamInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
150
    }
151
}
152