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 ( a26bc3...da45b8 )
by Simon
03:36
created

LaravelEventStore::deserializeEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 10
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\EventStore\EventStreamNotFound;
12
use SmoothPhp\Contracts\Serialization\Serializer;
13
use SmoothPhp\Domain\DateTime;
14
use SmoothPhp\EventStore\DuplicateAggregatePlayhead;
15
use SmoothPhp\EventStore\EventStreamNotFound;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Cannot use SmoothPhp\EventStore\EventStreamNotFound as EventStreamNotFound because the name is already in use
Loading history...
16
17
/**
18
 * Class LaravelEventStore
19
 * @package SmoothPhp\LaravelAdapter\EventStore
20
 * @author Simon Bennett <[email protected]>
21
 */
22
final class LaravelEventStore implements EventStore
23
{
24
    /** @var Serializer */
25
    private $serializer;
26
27
    /** @var string */
28
    private $eventStoreTableName;
29
30
    /** @var Connection */
31
    private $db;
32
33
    /**
34
     * @param DatabaseManager $databaseManager
35
     * @param Serializer $serializer
36
     * @param string $eventStoreConnectionName
37
     * @param string $eventStoreTableName
38
     */
39
    public function __construct(
40
        DatabaseManager $databaseManager,
41
        Serializer $serializer,
42
        $eventStoreConnectionName,
43
        $eventStoreTableName
44
    ) {
45
        $this->db = $databaseManager->connection($eventStoreConnectionName);
46
        $this->serializer = $serializer;
47
        $this->eventStoreTableName = $eventStoreTableName;
48
    }
49
50
    /**
51
     * @param string $id
52
     * @return DomainEventStream
53
     * @throws EventStreamNotFound
54
     */
55
    public function load($id)
56
    {
57
        $rows = $this->db->table($this->eventStoreTableName)
58
                         ->select(['uuid', 'playhead', 'metadata', 'payload', 'recorded_on'])
59
                         ->where('uuid', $id)
60
                         ->orderBy('playhead', 'asc')
61
                         ->get();
62
        $events = [];
63
64
        foreach ($rows as $row) {
65
            $events[] = $this->deserializeEvent($row);
66
        }
67
68
        if (empty($events)) {
69
            throw new EventStreamNotFound(sprintf('EventStream not found for aggregate with id %s', $id));
70
        }
71
72
        return new \SmoothPhp\Domain\DomainEventStream($events);
73
    }
74
75
    /**
76
     * @param mixed $id
77
     * @param DomainEventStream $eventStream
78
     */
79
    public function append($id, DomainEventStream $eventStream)
80
    {
81
        $id = (string)$id; //Used to thrown errors if ID will not cast to string
82
83
        $this->db->beginTransaction();
84
85
        try {
86
            foreach ($eventStream as $domainMessage) {
87
                $this->insertEvent($domainMessage);
88
            }
89
90
            $this->db->commit();
91
        } catch (QueryException $ex) {
92
            $this->db->rollBack();
93
94
            throw  $ex;
95
        }
96
    }
97
98
    /**
99
     * @param DomainMessage $domainMessage
100
     */
101
    private function insertEvent(DomainMessage $domainMessage)
102
    {
103
        try {
104
            $this->db->table($this->eventStoreTableName)
105
                     ->insert(
106
                         [
107
                             'uuid'        => (string)$domainMessage->getId(),
108
                             'playhead'    => $domainMessage->getPlayHead(),
109
                             'metadata'    => json_encode($this->serializer->serialize($domainMessage->getMetadata())),
110
                             'payload'     => json_encode($this->serializer->serialize($domainMessage->getPayload())),
111
                             'recorded_on' => (string)$domainMessage->getRecordedOn(),
112
                             'type'        => $domainMessage->getType(),
113
                         ]
114
                     );
115
        } catch (\PDOException $ex) {
116
            if ($ex->getCode() == 23000) {
117
                throw new DuplicateAggregatePlayhead((string)$domainMessage->getId(), $domainMessage->getPlayHead());
118
            }
119
            throw  $ex;
120
        }
121
    }
122
123
    /**
124
     * @param \stdClass
125
     * @return DomainMessage
126
     */
127
    private function deserializeEvent($row)
128
    {
129
        return new \SmoothPhp\Domain\DomainMessage(
130
            $row->uuid,
131
            $row->playhead,
132
            $this->serializer->deserialize(json_decode($row->metadata, true)),
133
            $this->serializer->deserialize(json_decode($row->payload, true)),
134
            new DateTime($row->recorded_on)
135
        );
136
    }
137
138
    /**
139
     * @param string[] $eventTypes
140
     * @return int
141
     */
142
    public function getEventCountByTypes($eventTypes)
143
    {
144
        return $this->db->table($this->eventStoreTableName)
145
                        ->whereIn('type', $eventTypes)
146
                        ->count();
147
    }
148
149
    /**
150
     * @param string[] $eventTypes
151
     * @param int $skip
152
     * @param int $take
153
     * @return DomainEventStream
154
     */
155
    public function getEventsByType($eventTypes, $skip, $take)
156
    {
157
        $rows = $this->db->table($this->eventStoreTableName)
158
                         ->select(['uuid', 'playhead', 'metadata', 'payload', 'recorded_on'])
159
                         ->whereIn('type', $eventTypes)
160
                         ->skip($skip)
161
                         ->take($take)
162
                         ->orderBy('recorded_on', 'asc')
163
                         ->get();
164
        $events = [];
165
166
        foreach ($rows as $row) {
167
            $events[] = $this->deserializeEvent($row);
168
        }
169
170
        return new \SmoothPhp\Domain\DomainEventStream($events);
171
    }
172
}