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.

RunProjectionCommand::handle()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 51
rs 9.069
c 0
b 0
f 0

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
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
     * @throws \Exception
73
     */
74
    public function handle()
75
    {
76
        $projectionRequest = collect(explode(',', $this->argument('projections')));
77
78
        $projectionsServiceProviders = $projectionRequest->each(
79
            function ($projectionName) {
80
                if (!isset($this->config->get('cqrses.projections_service_providers')[$projectionName])) {
81
                    $this->error("{$projectionName} Does not exist, check cqrses config");
82
83
                    exit();
84
                }
85
            }
86
        )->map(
87
            function ($projectionName) {
88
                return $this->application->make(
89
                    $this->config->get('cqrses.projections_service_providers')[$projectionName]
90
                );
91
            }
92
        )->each(
93
            function (ProjectionServiceProvider $projectionClass) {
94
                $this->downMigration($projectionClass);
95
            }
96
        )->each(
97
            function (ProjectionServiceProvider $projectionClass) {
98
                $this->upMigration($projectionClass);
99
            }
100
        );
101
102
        /** @var Collection|Subscriber[] $projections */
103
        $projections = $projectionsServiceProviders->map(
104
            function (ProjectionServiceProvider $projectServiceProvider) {
105
                return collect($projectServiceProvider->getProjections())->map(
106
                    function ($projection) {
107
                        return $this->application->make($projection);
108
                    }
109
                );
110
            }
111
        )->collapse();
112
113
        $events = $projections->map(
114
            function (Subscriber $subscriber) {
115
                return array_keys($subscriber->getSubscribedEvents());
116
            }
117
        )->collapse()->map(
118
            function ($eventClassName) {
119
                return str_replace('\\', '.', $eventClassName);
120
            }
121
        );
122
123
        $this->replayEvents($projections, $events->toArray());
124
    }
125
126
    /**
127
     * @param ProjectionServiceProvider $projectionServiceProvider
128
     */
129
    public function downMigration(ProjectionServiceProvider $projectionServiceProvider)
130
    {
131
        $response = $projectionServiceProvider->down();
132
        $this->line($response ?? 'Migrated Down: ' . \get_class($projectionServiceProvider));
133
    }
134
135
    /**
136
     * @param ProjectionServiceProvider $projectionServiceProvider
137
     */
138
    public function upMigration(ProjectionServiceProvider $projectionServiceProvider)
139
    {
140
        $response = $projectionServiceProvider->up();
141
        $this->line($response ?? 'Migrated Up: ' . \get_class($projectionServiceProvider));
142
    }
143
144
    /**
145
     * @param Collection|Subscriber[]
146
     * @param string[] $events
147
     *
148
     * @throws \Exception
149
     */
150
    protected function replayEvents($projections, $events)
151
    {
152
        $eventCount = $this->eventStore->getEventCountByTypes($events);
153
        $take = (int)$this->config->get('cqrses.rebuild_transaction_size', 1000);
154
155
        $this->output->progressStart($eventCount);
156
        $dispatcher = $this->buildAndRegisterDispatcher($projections);
157
158
        /** @var DomainEventStream $eventStream */
159
        foreach ($this->eventStore->getEventsByType($events, $take) as $eventStream) {
160
            if ($this->option('transactions')) {
161
                $this->databaseManager->connection()->beginTransaction();
162
            }
163
            foreach ($eventStream as $eventRow) {
164
                $this->dispatchEvent($dispatcher, $eventRow);
165
            }
166
            if ($this->option('transactions')) {
167
                $this->databaseManager->connection()->commit();
168
            }
169
            $this->output->progressAdvance($take);
170
        }
171
172
        $this->output->progressFinish();
173
        $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...
174
    }
175
176
    /**
177
     * @param EventDispatcher $eventDispatcher
178
     * @param $eventRow
179
     */
180
    protected function dispatchEvent(EventDispatcher $eventDispatcher, DomainMessage $eventRow)
181
    {
182
        $eventDispatcher->dispatch(
183
            $eventRow->getType(),
184
            [
185
                $eventRow->getPayload(),
186
            ],
187
            true
188
        );
189
    }
190
191
    /**
192
     * @param Collection $projections
193
     * @return EventDispatcher
194
     */
195
    protected function buildAndRegisterDispatcher($projections)
196
    {
197
        /** @var EventDispatcher $dispatcher */
198
        $dispatcher = $this->application->make(
199
            $this->config->get(
200
                'cqrses.rebuild_event_dispatcher',
201
                $this->config->get('cqrses.event_dispatcher')
202
            )
203
        );
204
205
        $projections->each(
206
            function ($projection) use ($dispatcher) {
207
                $dispatcher->addSubscriber($projection);
208
            }
209
        );
210
211
        return $dispatcher;
212
    }
213
}
214