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 ( da45b8...91983c )
by Simon
03:00
created

LaravelEventStore::append()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 5
nop 2
dl 0
loc 18
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
     */
78
    public function append($id, DomainEventStream $eventStream)
79
    {
80
        $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...
81
82
        $this->db->beginTransaction();
83
84
        try {
85
            foreach ($eventStream as $domainMessage) {
86
                $this->insertEvent($domainMessage);
87
            }
88
89
            $this->db->commit();
90
        } catch (QueryException $ex) {
91
            $this->db->rollBack();
92
93
            throw  $ex;
94
        }
95
    }
96
97
    /**
98
     * @param DomainMessage $domainMessage
99
     */
100
    private function insertEvent(DomainMessage $domainMessage)
101
    {
102
        try {
103
            $this->db->table($this->eventStoreTableName)
104
                     ->insert(
105
                         [
106
                             'uuid'        => (string)$domainMessage->getId(),
107
                             'playhead'    => $domainMessage->getPlayHead(),
108
                             'metadata'    => json_encode($this->serializer->serialize($domainMessage->getMetadata())),
109
                             'payload'     => json_encode($this->serializer->serialize($domainMessage->getPayload())),
110
                             'recorded_on' => (string)$domainMessage->getRecordedOn(),
111
                             'type'        => $domainMessage->getType(),
112
                         ]
113
                     );
114
        } catch (\PDOException $ex) {
115
            if ($ex->getCode() == 23000) {
116
                throw new DuplicateAggregatePlayhead((string)$domainMessage->getId(), $domainMessage->getPlayHead());
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
}