GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 9ae1b6...7b2ad3 )
by Simon
04:38
created

RunProjectionCommand::handle()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 30
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 52
rs 9.4929

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 declare (strict_types = 1);
2
namespace SmoothPhp\LaravelAdapter\Console;
3
4
use Illuminate\Console\Command;
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Contracts\Foundation\Application;
7
use Illuminate\Support\Collection;
8
use SmoothPhp\Contracts\Domain\DomainMessage;
9
use SmoothPhp\Contracts\EventDispatcher\EventDispatcher;
10
use SmoothPhp\Contracts\EventDispatcher\Subscriber;
11
use SmoothPhp\Contracts\EventStore\EventStore;
12
use SmoothPhp\Contracts\Projections\ProjectionServiceProvider;
13
14
/**
15
 * Class RunProjectionCommand
16
 * @package SmoothPhp\LaravelAdapter\Console
17
 * @author Simon Bennett <[email protected]>
18
 */
19
final class RunProjectionCommand extends Command
20
{
21
    /**
22
     * The name and signature of the console command.
23
     *
24
     * @var string
25
     */
26
    protected $signature = 'smoothphp:project {projections}';
27
28
    /**
29
     * The console command description.
30
     *
31
     * @var string
32
     */
33
    protected $description = 'Run Projections';
34
35
    /** @var Repository */
36
    private $config;
37
38
    /** @var EventDispatcher */
39
    private $eventDispatcher;
40
41
    /** @var EventStore */
42
    private $eventStore;
43
    /** @var Application */
44
    private $application;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
45
46
    /**
47
     * RunProjectionCommand constructor.
48
     * @param Repository $config
49
     * @param EventDispatcher $eventDispatcher
50
     * @param EventStore $eventStore
51
     * @param Application $application
52
     */
53
    public function __construct(
54
        Repository $config,
55
        EventDispatcher $eventDispatcher,
56
        EventStore $eventStore,
57
        Application $application
58
    ) {
59
        parent::__construct();
60
        $this->config = $config;
61
        $this->eventDispatcher = $eventDispatcher;
62
        $this->eventStore = $eventStore;
63
        $this->application = $application;
64
    }
65
66
    /**
67
     * Execute the console command.
68
     *
69
     * @return mixed
70
     */
71
    public function handle()
72
    {
73
        $projectionRequest = collect(explode(',', $this->argument('projections')));
74
75
        $projectionsServiceProviders = $projectionRequest->each(
76
            function ($projectionName) {
77
                if (!isset($this->config->get('cqrses.projections_service_providers')[$projectionName])) {
78
                    $this->error("{$projectionName} Does not exist, check cqrses config");
79
80
                    exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method handle() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
81
                }
82
            }
83
        )->map(
84
            function ($projectionName) {
85
                return $this->application->make(
86
                    $this->config->get('cqrses.projections_service_providers')[$projectionName]
87
                );
88
            }
89
        )->each(
90
            function (ProjectionServiceProvider $projectionClass) {
91
                $this->downMigration($projectionClass);
92
            }
93
        )->each(
94
            function (ProjectionServiceProvider $projectionClass) {
95
                $this->upMigration($projectionClass);
96
            }
97
        );
98
99
        /** @var Collection|Subscriber[] $projections */
100
        $projections = $projectionsServiceProviders->map(
101
            function (ProjectionServiceProvider $projectServiceProvider) {
102
                return collect($projectServiceProvider->getProjections())->map(
103
                    function ($projection) {
104
                        return $this->application->make($projection);
105
                    }
106
                );
107
            }
108
        )->collapse();
109
110
        $events = $projections->map(
111
            function (Subscriber $subscriber) {
112
                return array_keys($subscriber->getSubscribedEvents());
113
            }
114
        )->collapse()->map(
115
            function ($eventClassName) {
116
                return str_replace('\\', '.', $eventClassName);
117
            }
118
        );
119
120
        $this->replayEvents($projections, $events);
121
122
    }
123
124
    /**
125
     * @param ProjectionServiceProvider $projectionServiceProvider
126
     */
127
    public function downMigration(ProjectionServiceProvider $projectionServiceProvider)
128
    {
129
        $this->line($projectionServiceProvider->down());
130
    }
131
132
    /**
133
     * @param ProjectionServiceProvider $projectionServiceProvider
134
     */
135
    public function upMigration(ProjectionServiceProvider $projectionServiceProvider)
136
    {
137
        $this->line($projectionServiceProvider->up());
138
    }
139
140
    /**
141
     * @param Collection|Subscriber[]
142
     * @param string[] $events
143
     */
144
    protected function replayEvents($projections, $events)
145
    {
146
        $eventCount = $this->eventStore->getEventCountByTypes($events);
147
        $start = 0;
148
        $take = 1000;
149
150
        $this->output->progressStart($eventCount);
151
        $dispatcher = $this->buildAndRegisterDispatcher($projections);
152
153
154
        while ($start < $eventCount) {
155
            foreach ($this->eventStore->getEventsByType($events, $start, $take) as $eventRow) {
156
                $this->dispatchEvent($dispatcher, $eventRow);
157
            }
158
            $start += $take;
159
            $this->output->progressAdvance($take > $eventCount ? $eventCount : $take);
160
        }
161
        $this->output->progressFinish();
162
        $this->line((memory_get_peak_usage(true) / 1024 / 1024) . "mb Peak Usage", false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|null.

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...
163
    }
164
165
    /**
166
     * @param EventDispatcher $eventDispatcher
167
     * @param $eventRow
168
     */
169
    protected function dispatchEvent(EventDispatcher $eventDispatcher, DomainMessage $eventRow)
170
    {
171
        $eventDispatcher->dispatch(
172
            $eventRow->getType(),
173
            [
174
                $eventRow->getPayload()
175
            ],
176
            true
177
        );
178
    }
179
180
    /**
181
     * @param Collection $projections
182
     * @return EventDispatcher
183
     */
184
    protected function buildAndRegisterDispatcher($projections)
185
    {
186
        /** @var EventDispatcher $dispatcher */
187
        $dispatcher = $this->application->make($this->config->get('cqrses.event_dispatcher'));
188
189
        $projections->each(
190
            function ($projection) use ($dispatcher) {
191
                $dispatcher->addSubscriber($projection);
192
            }
193
        );
194
195
        return $dispatcher;
196
    }
197
}
198