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 ( 5df319...df6192 )
by Simon
02:40
created

RebuildProjectionsCommand::replayEvents()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 21
rs 9.3142
cc 3
eloc 14
nc 3
nop 0
1
<?php
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\Database\DatabaseManager;
8
use SmoothPhp\Contracts\EventDispatcher\EventDispatcher;
9
use SmoothPhp\Contracts\Serialization\Serializer;
10
use SmoothPhp\Serialization\Exception\SerializedClassDoesNotExist;
11
12
/**
13
 * Class RebuildProjectionsCommand
14
 * @package SmoothPhp\LaravelAdapter\Console
15
 * @author Simon Bennett <[email protected]>
16
 */
17
final class RebuildProjectionsCommand extends Command
18
{
19
    /**
20
     * The name and signature of the console command.
21
     *
22
     * @var string
23
     */
24
    protected $signature = 'smoothphp:rebuild';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Rebuild all projection';
32
33
    /** @var Repository */
34
    private $config;
35
36
    /**
37
     * @var EventDispatcher
38
     */
39
    private $dispatcher;
40
41
    /** @var DatabaseManager */
42
    private $databaseManager;
43
44
    /** @var Serializer */
45
    private $serializer;
46
47
48
    /**
49
     * BuildLaravelEventStore constructor.
50
     * @param Repository $config
51
     * @param Application $application
52
     * @param DatabaseManager $databaseManager
53
     * @param Serializer $serializer
54
     */
55
    public function __construct(
56
        Repository $config,
57
        Application $application,
58
        DatabaseManager $databaseManager,
59
        Serializer $serializer
60
    ) {
61
        parent::__construct();
62
        $this->config = $config;
63
64
        $this->dispatcher = $application->make(EventDispatcher::class);
65
        $this->databaseManager = $databaseManager;
66
        $this->serializer = $serializer;
67
    }
68
69
    /**
70
     * Execute the console command.
71
     *
72
     * @return mixed
73
     */
74
    public function handle()
75
    {
76
        foreach ($this->config->get('cqrses.pre_rebuild_commands') as $preRebuildCommand) {
77
            if (is_array($preRebuildCommand)) {
78
                $this->call(key($preRebuildCommand), current($preRebuildCommand));
79
            } else {
80
                $this->call($preRebuildCommand);
81
            }
82
        }
83
84
        $this->replayEvents();
85
86
        foreach ($this->config->get('cqrses.post_rebuild_commands') as $postRebuildCommand) {
87
            if (is_array($postRebuildCommand)) {
88
                $this->call(key($postRebuildCommand), current($postRebuildCommand));
89
            } else {
90
                $this->call($postRebuildCommand);
91
            }
92
        }
93
    }
94
95
    /**
96
     *
97
     */
98
    protected function replayEvents()
99
    {
100
        $eventCount = $this->getEventCount();
101
        $start = 0;
102
        $take = 1000;
103
        $this->output->progressStart($eventCount);
104
105
        while ($start < $eventCount) {
106
            $this->databaseManager->connection(config('database.default'))->beginTransaction();
107
            foreach ($this->getFromEventStore($start, $take) as $eventRow) {
0 ignored issues
show
Bug introduced by
The expression $this->getFromEventStore($start, $take) of type object<stdClass> is not traversable.
Loading history...
108
                $this->dispatchEvent($eventRow);
109
            }
110
            $this->databaseManager->connection(config('database.default'))->commit();
111
112
            $start += $take;
113
            $this->output->progressAdvance($take);
114
        }
115
        $this->output->progressFinish();
116
117
        $this->output->write((memory_get_peak_usage(true) / 1024 / 1024) . " MiB", false);
118
    }
119
120
    /**
121
     * @param $start
122
     * @param $take
123
     * @return \stdClass
124
     */
125
    private function getFromEventStore($start, $take)
126
    {
127
        return $this->databaseManager
128
            ->connection($this->config->get('cqrses.eventstore_connection'))
129
            ->table($this->config->get('cqrses.eventstore_table'))
130
            ->take($take)
131
            ->skip($start)
132
            ->get();
133
    }
134
135
    /**
136
     * @return int
137
     */
138
    private function getEventCount()
139
    {
140
        return $this->databaseManager
141
            ->connection($this->config->get('cqrses.eventstore_connection'))
142
            ->table($this->config->get('cqrses.eventstore_table'))
143
            ->count();
144
    }
145
146
    /**
147
     * @param $eventRow
148
     */
149
    protected function dispatchEvent($eventRow)
150
    {
151
        $this->dispatcher->dispatch(
152
            $eventRow->type,
153
            [
154
                (call_user_func(
155
                    [
156
                        str_replace('.', '\\', $eventRow->type),
157
                        'deserialize'
158
                    ],
159
                    json_decode($eventRow->payload, true)['payload']
160
                ))
161
            ],
162
            true
163
        );
164
    }
165
}
166