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 ( f97525...21e2f9 )
by Simon
05:11
created

RunProjectionCommand::replayEvents()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
c 0
b 0
f 0
nc 9
nop 2
dl 0
loc 25
rs 8.439
1
<?php declare(strict_types=1);
2
3
namespace SmoothPhp\LaravelAdapter\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Contracts\Config\Repository;
7
use Illuminate\Contracts\Foundation\Application;
8
use Illuminate\Database\DatabaseManager;
9
use Illuminate\Support\Collection;
10
use SmoothPhp\Contracts\Domain\DomainMessage;
11
use SmoothPhp\Contracts\EventDispatcher\EventDispatcher;
12
use SmoothPhp\Contracts\EventDispatcher\Subscriber;
13
use SmoothPhp\Contracts\EventStore\EventStore;
14
use SmoothPhp\Contracts\Projections\ProjectionServiceProvider;
15
use SmoothPhp\Domain\DomainEventStream;
16
17
/**
18
 * Class RunProjectionCommand
19
 * @package SmoothPhp\LaravelAdapter\Console
20
 * @author Simon Bennett <[email protected]>
21
 */
22
final class RunProjectionCommand extends Command
23
{
24
    /**
25
     * The name and signature of the console command.
26
     *
27
     * @var string
28
     */
29
    protected $signature = 'smoothphp:project {projections} {--transactions}';
30
31
    /**
32
     * The console command description.
33
     *
34
     * @var string
35
     */
36
    protected $description = 'Run Projections';
37
38
    /** @var Repository */
39
    private $config;
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
    /** @var DatabaseManager */
46
    private $databaseManager;
47
48
    /**
49
     * RunProjectionCommand constructor.
50
     * @param Repository $config
51
     * @param EventStore $eventStore
52
     * @param Application $application
53
     * @param DatabaseManager $databaseManager
54
     */
55
    public function __construct(
56
        Repository $config,
57
        EventStore $eventStore,
58
        Application $application,
59
        DatabaseManager $databaseManager
60
    ) {
61
        parent::__construct();
62
        $this->config = $config;
63
        $this->eventStore = $eventStore;
64
        $this->application = $application;
65
        $this->databaseManager = $databaseManager;
66
    }
67
68
    /**
69
     * Execute the console command.
70
     *
71
     * @return mixed
72
     */
73
    public function handle()
74
    {
75
        $projectionRequest = collect(explode(',', $this->argument('projections')));
76
77
        $projectionsServiceProviders = $projectionRequest->each(
78
            function ($projectionName) {
79
                if (!isset($this->config->get('cqrses.projections_service_providers')[$projectionName])) {
80
                    $this->error("{$projectionName} Does not exist, check cqrses config");
81
82
                    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...
83
                }
84
            }
85
        )->map(
86
            function ($projectionName) {
87
                return $this->application->make(
88
                    $this->config->get('cqrses.projections_service_providers')[$projectionName]
89
                );
90
            }
91
        )->each(
92
            function (ProjectionServiceProvider $projectionClass) {
93
                $this->downMigration($projectionClass);
94
            }
95
        )->each(
96
            function (ProjectionServiceProvider $projectionClass) {
97
                $this->upMigration($projectionClass);
98
            }
99
        );
100
101
        /** @var Collection|Subscriber[] $projections */
102
        $projections = $projectionsServiceProviders->map(
103
            function (ProjectionServiceProvider $projectServiceProvider) {
104
                return collect($projectServiceProvider->getProjections())->map(
105
                    function ($projection) {
106
                        return $this->application->make($projection);
107
                    }
108
                );
109
            }
110
        )->collapse();
111
112
        $events = $projections->map(
113
            function (Subscriber $subscriber) {
114
                return array_keys($subscriber->getSubscribedEvents());
115
            }
116
        )->collapse()->map(
117
            function ($eventClassName) {
118
                return str_replace('\\', '.', $eventClassName);
119
            }
120
        );
121
122
        $this->replayEvents($projections, $events->toArray());
123
    }
124
125
    /**
126
     * @param ProjectionServiceProvider $projectionServiceProvider
127
     */
128
    public function downMigration(ProjectionServiceProvider $projectionServiceProvider)
129
    {
130
        $this->line($projectionServiceProvider->down());
131
    }
132
133
    /**
134
     * @param ProjectionServiceProvider $projectionServiceProvider
135
     */
136
    public function upMigration(ProjectionServiceProvider $projectionServiceProvider)
137
    {
138
        $this->line($projectionServiceProvider->up());
139
    }
140
141
    /**
142
     * @param Collection|Subscriber[]
143
     * @param string[] $events
144
     */
145
    protected function replayEvents($projections, $events)
146
    {
147
        $eventCount = $this->eventStore->getEventCountByTypes($events);
148
        $take = (int)$this->config->get('cqrses.rebuild_transaction_size', 1000);
149
150
        $this->output->progressStart($eventCount);
151
        $dispatcher = $this->buildAndRegisterDispatcher($projections);
152
153
        /** @var DomainEventStream $eventStream */
154
        foreach ($this->eventStore->getEventsByType($events, $take) as $eventStream) {
155
            if ($this->option('transactions')) {
156
                $this->databaseManager->connection()->beginTransaction();
157
            }
158
            foreach ($eventStream as $eventRow) {
159
                $this->dispatchEvent($dispatcher, $eventRow);
160
            }
161
            if ($this->option('transactions')) {
162
                $this->databaseManager->connection()->commit();
163
            }
164
            $this->output->progressAdvance($take);
165
        }
166
167
        $this->output->progressFinish();
168
        $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...
169
    }
170
171
    /**
172
     * @param EventDispatcher $eventDispatcher
173
     * @param $eventRow
174
     */
175
    protected function dispatchEvent(EventDispatcher $eventDispatcher, DomainMessage $eventRow)
176
    {
177
        $eventDispatcher->dispatch(
178
            $eventRow->getType(),
179
            [
180
                $eventRow->getPayload(),
181
            ],
182
            true
183
        );
184
    }
185
186
    /**
187
     * @param Collection $projections
188
     * @return EventDispatcher
189
     */
190
    protected function buildAndRegisterDispatcher($projections)
191
    {
192
        /** @var EventDispatcher $dispatcher */
193
        $dispatcher = $this->application->make(
194
            $this->config->get(
195
                'cqrses.rebuild_event_dispatcher',
196
                $this->config->get('cqrses.event_dispatcher')
197
            )
198
        );
199
200
        $projections->each(
201
            function ($projection) use ($dispatcher) {
202
                $dispatcher->addSubscriber($projection);
203
            }
204
        );
205
206
        return $dispatcher;
207
    }
208
}
209