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 ( 779ac4...87aa82 )
by Simon
02:16
created

LaravelEventStore   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 12.84 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 9
c 4
b 1
f 0
lcom 1
cbo 9
dl 14
loc 109
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A load() 0 19 3
A append() 0 18 3
A insertEvent() 14 14 1
A deserializeEvent() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
15
/**
16
 * Class LaravelEventStore
17
 * @package SmoothPhp\LaravelAdapter\EventStore
18
 * @author Simon Bennett <[email protected]>
19
 */
20
final class LaravelEventStore implements EventStore
21
{
22
    /** @var Serializer */
23
    private $serializer;
24
25
    /** @var string */
26
    private $eventStoreTableName;
27
28
    /** @var Connection */
29
    private $db;
30
31
    /**
32
     * @param DatabaseManager $databaseManager
33
     * @param Serializer $serializer
34
     * @param string $eventStoreConnectionName
35
     * @param string $eventStoreTableName
36
     */
37
    public function __construct(
38
        DatabaseManager $databaseManager,
39
        Serializer $serializer,
40
        $eventStoreConnectionName,
41
        $eventStoreTableName
42
    ) {
43
        $this->db = $databaseManager->connection($eventStoreConnectionName);
44
        $this->serializer = $serializer;
45
        $this->eventStoreTableName = $eventStoreTableName;
46
    }
47
48
    /**
49
     * @param string $id
50
     * @return DomainEventStream
51
     * @throws EventStreamNotFound
52
     */
53
    public function load($id)
54
    {
55
        $rows = $this->db->table($this->eventStoreTableName)
56
                         ->select(['uuid', 'playhead', 'metadata', 'payload', 'recorded_on'])
57
                         ->where('uuid', $id)
58
                         ->orderBy('playhead', 'asc')
59
                         ->get();
60
        $events = [];
61
62
        foreach ($rows as $row) {
63
            $events[] = $this->deserializeEvent($row);
64
        }
65
66
        if (empty($events)) {
67
            throw new EventStreamNotFound(sprintf('EventStream not found for aggregate with id %s', $id));
68
        }
69
70
        return new \SmoothPhp\Domain\DomainEventStream($events);
71
    }
72
73
    /**
74
     * @param mixed $id
75
     * @param DomainEventStream $eventStream
76
     */
77
    public function append($id, DomainEventStream $eventStream)
78
    {
79
        $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...
80
81
        $this->db->beginTransaction();
82
83
        try {
84
            foreach ($eventStream as $domainMessage) {
85
                $this->insertEvent( $domainMessage);
86
            }
87
88
            $this->db->commit();
89
        } catch (QueryException $ex) {
90
            $this->db->rollBack();
91
92
            throw  $ex;
93
        }
94
    }
95
96
    /**
97
     * @param DomainMessage $domainMessage
98
     */
99 View Code Duplication
    private function insertEvent(DomainMessage $domainMessage)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $this->db->table($this->eventStoreTableName)
102
           ->insert(
103
               [
104
                   'uuid'        => (string)$domainMessage->getId(),
105
                   'playhead'    => $domainMessage->getPlayHead(),
106
                   'metadata'    => json_encode($this->serializer->serialize($domainMessage->getMetadata())),
107
                   'payload'     => json_encode($this->serializer->serialize($domainMessage->getPayload())),
108
                   'recorded_on' => (string)$domainMessage->getRecordedOn(),
109
                   'type'        => $domainMessage->getType(),
110
               ]
111
           );
112
    }
113
114
    /**
115
     * @param \stdClass
116
     * @return DomainMessage
117
     */
118
    private function deserializeEvent($row)
119
    {
120
        return new \SmoothPhp\Domain\DomainMessage(
121
            $row->uuid,
122
            $row->playhead,
123
            $this->serializer->deserialize(json_decode($row->metadata, true)),
124
            $this->serializer->deserialize(json_decode($row->payload, true)),
125
            new DateTime($row->recorded_on)
126
        );
127
    }
128
}