Completed
Push — master ( dc5b69...0eb8a8 )
by Michiel
05:16 queued 02:56
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\PastEventsService;
24
use Surfnet\StepupMiddleware\MiddlewareBundle\Service\TransactionAwareEventDispatcher;
25
use Symfony\Component\Console\Command\Command;
26
use Symfony\Component\Console\Helper\QuestionHelper;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
use Symfony\Component\Console\Question\ChoiceQuestion;
31
32
class ReplaySpecificEventsCommand extends Command
33
{
34
    const OPTION_LIST_EVENTS = 'list-events';
35
    const OPTION_LIST_PROJECTORS = 'list-projectors';
36
37
    /**
38
     * @var EventCollection
39
     */
40
    private $collection;
41
42
    /**
43
     * @var PastEventsService
44
     */
45
    private $pastEventsService;
46
47
    /**
48
     * @var TransactionAwareEventDispatcher
49
     */
50
    private $eventDispatcher;
51
    /**
52
     * @var ProjectorCollection
53
     */
54
    private $projectorCollection;
55
56
    protected function configure()
57
    {
58
        $this
59
            ->setName('stepup:event:replay')
60
            ->setDescription('replay specified events for specified projectors')
61
            ->addOption(
62
                self::OPTION_LIST_EVENTS,
63
                null,
64
                InputOption::VALUE_NONE,
65
                'List all events available to replay'
66
            )
67
            ->addOption(
68
                self::OPTION_LIST_PROJECTORS,
69
                null,
70
                InputOption::VALUE_NONE,
71
                'List all projectors available for which events can be replayed'
72
            );
73
    }
74
75
    public function __construct(
76
        EventCollection $collection,
77
        ProjectorCollection $projectorCollection,
78
        PastEventsService $pastEventsService,
79
        TransactionAwareEventDispatcher $eventDispatcher
80
    ) {
81
        $this->collection = $collection;
82
        $this->projectorCollection = $projectorCollection;
83
        $this->pastEventsService = $pastEventsService;
84
        $this->eventDispatcher = $eventDispatcher;
85
        parent::__construct();
86
    }
87
88
    protected function execute(InputInterface $input, OutputInterface $output)
89
    {
90
        $availableEvents     = $this->collection->getEventNames();
91
        $availableProjectors = $this->projectorCollection->getProjectorNames();
92
93 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...
94
            $output->writeln('<info>The following events can be replayed:</info>');
95
            $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...
96
97
            return;
98
        }
99
100 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...
101
            $output->writeln('<info>Events can be replayed for the following projectors:</info>');
102
            $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...
103
104
            return;
105
        }
106
107
        if (count($availableProjectors) === 0) {
108
            $output->writeln('<error>There are no projectors configured to reply events for</error>');
109
110
            return;
111
        }
112
113
        /** @var QuestionHelper $questionHelper */
114
        $questionHelper = $this->getHelper('question');
115
116
        $selectEventsQuestion = new ChoiceQuestion(
117
            'Which events would you like to replay? Please supply a comma-separated list of numbers.',
118
            $availableEvents
119
        );
120
        $selectEventsQuestion->setMultiselect(true);
121
122
        $chosenEvents   = $questionHelper->ask($input, $output, $selectEventsQuestion);
123
        $eventSelection = $this->collection->select($chosenEvents);
124
125
        $selectProjectorsQuestion = new ChoiceQuestion(
126
            'For which projectors would you like to replay the selected events? '
127
            . 'Please supply a comma-separated list of numbers.',
128
            $availableProjectors
129
        );
130
        $selectProjectorsQuestion->setMultiselect(true);
131
132
        $chosenProjectors   = $questionHelper->ask($input, $output, $selectProjectorsQuestion);
133
        $projectorSelection = $this->projectorCollection->selectByNames($chosenProjectors);
134
135
        $events = $this->pastEventsService->findEventsBy($eventSelection);
136
137
        $output->writeln('<info>Registering projectors</info>');
138
        foreach ($projectorSelection as $projector) {
139
            $this->eventDispatcher->registerProjector($projector);
140
        }
141
142
        $output->writeln('<info>Dispatching events</info>');
143
        $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...
144
    }
145
}
146