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 ( 91983c...37f60a )
by Simon
05:11
created

LaravelEventStore::domainMessageToArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace SmoothPhp\LaravelAdapter\EventStore;
3
4
use Illuminate\Database\Connection;
5
use Illuminate\Database\DatabaseManager;
6
use Illuminate\Database\QueryException;
7
use SmoothPhp\Contracts\Domain\DomainEventStream;
8
use SmoothPhp\Contracts\Domain\DomainMessage;
9
use SmoothPhp\Contracts\EventStore\DomainEventStreamInterface;
10
use SmoothPhp\Contracts\EventStore\EventStore;
11
use SmoothPhp\Contracts\Serialization\Serializer;
12
use SmoothPhp\Domain\DateTime;
13
use SmoothPhp\EventStore\DuplicateAggregatePlayhead;
14
use SmoothPhp\EventStore\EventStreamNotFound;
15
16
/**
17
 * Class LaravelEventStore
18
 * @package SmoothPhp\LaravelAdapter\EventStore
19
 * @author Simon Bennett <[email protected]>
20
 */
21
final class LaravelEventStore implements EventStore
22
{
23
    /** @var Serializer */
24
    private $serializer;
25
26
    /** @var string */
27
    private $eventStoreTableName;
28
29
    /** @var Connection */
30
    private $db;
31
32
    /**
33
     * @param DatabaseManager $databaseManager
34
     * @param Serializer $serializer
35
     * @param string $eventStoreConnectionName
36
     * @param string $eventStoreTableName
37
     */
38
    public function __construct(
39
        DatabaseManager $databaseManager,
40
        Serializer $serializer,
41
        $eventStoreConnectionName,
42
        $eventStoreTableName
43
    ) {
44
        $this->db = $databaseManager->connection($eventStoreConnectionName);
45
        $this->serializer = $serializer;
46
        $this->eventStoreTableName = $eventStoreTableName;
47
    }
48
49
    /**
50
     * @param string $id
51
     * @return DomainEventStream
52
     * @throws EventStreamNotFound
53
     */
54
    public function load($id)
55
    {
56
        $rows = $this->db->table($this->eventStoreTableName)
57
                         ->select(['uuid', 'playhead', 'metadata', 'payload', 'recorded_on'])
58
                         ->where('uuid', $id)
59
                         ->orderBy('playhead', 'asc')
60
                         ->get();
61
        $events = [];
62
63
        foreach ($rows as $row) {
64
            $events[] = $this->deserializeEvent($row);
65
        }
66
67
        if (empty($events)) {
68
            throw new EventStreamNotFound(sprintf('EventStream not found for aggregate with id %s', $id));
69
        }
70
71
        return new \SmoothPhp\Domain\DomainEventStream($events);
72
    }
73
74
    /**
75
     * @param mixed $id
76
     * @param DomainEventStream $eventStream
77
     * @param bool $ignorePlayhead
78
     * @throws \PDOException
79
     * @throws \SmoothPhp\EventStore\DuplicateAggregatePlayhead
80
     * @throws \Illuminate\Database\QueryException
81
     */
82
    public function append($id, DomainEventStream $eventStream, bool $ignorePlayhead = false)
83
    {
84
        $id = (string)$id; //Used to thrown errors if ID will not cast to string
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85
86
        $this->db->beginTransaction();
87
88
        try {
89
            foreach ($eventStream as $domainMessage) {
90
                $this->insertEvent($this->domainMessageToArray($domainMessage), $ignorePlayhead);
91
            }
92
93
            $this->db->commit();
94
        } catch (QueryException $ex) {
95
            $this->db->rollBack();
96
97
            throw  $ex;
98
        }
99
    }
100
101
    /**
102
     * @param array $eventRow
103
     * @param bool $ignorePlayhead
104
     * @throws DuplicateAggregatePlayhead
105
     */
106
    private function insertEvent(array $eventRow, bool $ignorePlayhead)
107
    {
108
        try {
109
            $this->db->table($this->eventStoreTableName)->insert($eventRow);
110
        } catch (\PDOException $ex) {
111
            if ((string) $ex->getCode() === '23000') {
112
                if ($ignorePlayhead) {
113
                    $eventRow['playhead'] ++;
114
                    return $this->insertEvent($eventRow, true);
115
                }
116
                throw new DuplicateAggregatePlayhead($eventRow['uuid'], $eventRow['playhead']);
117
            }
118
            throw $ex;
119
        }
120
    }
121
122
    /**
123
     * @param \stdClass
124
     * @return DomainMessage
125
     */
126
    private function deserializeEvent($row)
127
    {
128
        return new \SmoothPhp\Domain\DomainMessage(
129
            $row->uuid,
130
            $row->playhead,
131
            $this->serializer->deserialize(json_decode($row->metadata, true)),
132
            $this->serializer->deserialize(json_decode($row->payload, true)),
133
            new DateTime($row->recorded_on)
134
        );
135
    }
136
137
    /**
138
     * @param string[] $eventTypes
139
     * @return int
140
     */
141
    public function getEventCountByTypes($eventTypes)
142
    {
143
        return $this->db->table($this->eventStoreTableName)
144
                        ->whereIn('type', $eventTypes)
145
                        ->count();
146
    }
147
148
    /**
149
     * @param string[] $eventTypes
150
     * @param int $skip
151
     * @param int $take
152
     * @return DomainEventStream
153
     */
154
    public function getEventsByType($eventTypes, $skip, $take)
155
    {
156
        $rows = $this->db->table($this->eventStoreTableName)
157
                         ->select(['uuid', 'playhead', 'metadata', 'payload', 'recorded_on'])
158
                         ->whereIn('type', $eventTypes)
159
                         ->skip($skip)
160
                         ->take($take)
161
                         ->orderBy('recorded_on', 'asc')
162
                         ->get();
163
        $events = [];
164
165
        foreach ($rows as $row) {
166
            $events[] = $this->deserializeEvent($row);
167
        }
168
169
        return new \SmoothPhp\Domain\DomainEventStream($events);
170
    }
171
172
    /**
173
     * @param DomainMessage $domainMessage
174
     * @return array
175
     */
176
    private function domainMessageToArray(DomainMessage $domainMessage): array
177
    {
178
        return [
179
            'uuid'        => (string)$domainMessage->getId(),
180
            'playhead'    => $domainMessage->getPlayHead(),
181
            'metadata'    => json_encode($this->serializer->serialize($domainMessage->getMetadata())),
182
            'payload'     => json_encode($this->serializer->serialize($domainMessage->getPayload())),
183
            'recorded_on' => (string)$domainMessage->getRecordedOn(),
184
            'type'        => $domainMessage->getType(),
185
        ];
186
    }
187
}