DbalEventStoreRepository   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 158
Duplicated Lines 100 %

Importance

Changes 0
Metric Value
dl 158
loc 158
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
B save() 33 33 2
A __construct() 3 3 1
A buildEventAggregate() 7 7 2
A count() 8 8 1
A buildEventAggregateAsArray() 16 16 2
A buildEventAggregateAsObject() 15 15 2
B byUuid() 24 24 2

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
/**
3
 * This file is part of the Simple EventStore Manager package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace SimpleEventStoreManager\Infrastructure\Persistence;
12
13
use Doctrine\DBAL\Connection;
14
use SimpleEventStoreManager\Domain\Model\Contracts\EventInterface;
15
use SimpleEventStoreManager\Domain\Model\Contracts\EventStoreRepositoryInterface;
16
use SimpleEventStoreManager\Domain\Model\Event;
17
use SimpleEventStoreManager\Domain\Model\AggregateUuid;
18
use SimpleEventStoreManager\Infrastructure\Drivers\PdoDriver;
19
20 View Code Duplication
class DbalEventStoreRepository implements EventStoreRepositoryInterface
0 ignored issues
show
Duplication introduced by
This class 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...
21
{
22
    /**
23
     * @var Connection
24
     */
25
    private $connection;
26
27
    /**
28
     * InMemoryEventRepository constructor.
29
     */
30
    public function __construct(Connection $connection)
31
    {
32
        $this->connection = $connection;
33
    }
34
35
    /**
36
     * @param AggregateUuid $uuid
37
     * @param int $returnType
38
     *
39
     * @return array|mixed|null
40
     */
41
    public function byUuid(AggregateUuid $uuid, $returnType = self::RETURN_AS_ARRAY)
42
    {
43
        $uuid = (string) $uuid;
44
        $query = 'SELECT
45
                  `uuid`,
46
                  `version`,
47
                  `payload`,
48
                  `type`,
49
                  `body`,
50
                  `occurred_on`
51
                FROM `'.PdoDriver::EVENTSTORE_TABLE_NAME.'` 
52
                WHERE `uuid` = :uuid
53
                ORDER BY `occurred_on` ASC';
54
        $stmt = $this->connection->prepare($query);
55
        $stmt->bindParam(':uuid', $uuid);
56
        $stmt->execute();
57
58
        $row = $stmt->fetchAll(\PDO::FETCH_ASSOC);
59
60
        if (!empty($row)) {
61
            return $this->buildEventAggregate($row, $returnType);
62
        }
63
64
        return null;
65
    }
66
67
    /**
68
     * @param array $rows
69
     * @param $returnType
70
     *
71
     * @return array
72
     */
73
    private function buildEventAggregate(array $rows, $returnType)
74
    {
75
        if ($returnType === self::RETURN_AS_ARRAY) {
76
            return $this->buildEventAggregateAsArray($rows);
77
        }
78
79
        return $this->buildEventAggregateAsObject($rows);
80
    }
81
82
    /**
83
     * @param array $rows
84
     * @return array
85
     */
86
    private function buildEventAggregateAsArray(array $rows)
87
    {
88
        $returnArray = [];
89
90
        foreach ($rows as $row) {
91
            $returnArray[] = [
92
                'uuid' => $row['uuid'],
93
                'version' => $row['version'],
94
                'payload' => $row['payload'],
95
                'type' => $row['type'],
96
                'body' => unserialize($row['body']),
97
                'occurred_on' => $row['occurred_on'],
98
            ];
99
        }
100
101
        return $returnArray;
102
    }
103
104
    /**
105
     * @param array $rows
106
     * @return array
107
     */
108
    private function buildEventAggregateAsObject(array $rows)
109
    {
110
        $returnObject = [];
111
112
        foreach ($rows as $row) {
113
            $returnObject[] = new Event(
114
                new AggregateUuid($row['uuid']),
115
                $row['type'],
116
                unserialize($row['body']),
117
                $row['version'],
118
                $row['occurred_on']
119
            );
120
        }
121
122
        return $returnObject;
123
    }
124
125
    /**
126
     * @param AggregateUuid $uuid
127
     *
128
     * @return int
129
     */
130
    public function count(AggregateUuid $uuid)
131
    {
132
        $sql = 'SELECT id FROM `'.PdoDriver::EVENTSTORE_TABLE_NAME.'` WHERE `uuid` = :uuid';
133
        $stmt = $this->connection->prepare($sql);
134
        $stmt->bindParam(':uuid', $uuid);
135
        $stmt->execute();
136
137
        return $stmt->rowCount();
138
    }
139
140
    /**
141
     * @param EventInterface $event
142
     *
143
     * @return mixed
144
     */
145
    public function save(EventInterface $event)
146
    {
147
        $uuid = (string) $event->uuid();
148
        $version = ($this->count($event->uuid())) ?: 0;
149
        $type = $event->type();
150
        $payload = $event->payload();
151
        $body = serialize($event->body());
152
        $occurredOn = $event->occurredOn()->format('Y-m-d H:i:s.u');
153
154
        $sql = 'INSERT INTO `'.PdoDriver::EVENTSTORE_TABLE_NAME.'` (
155
                    `uuid`,
156
                    `version`,
157
                    `payload`,
158
                    `type`,
159
                    `body`,
160
                    `occurred_on`
161
                  ) VALUES (
162
                    :uuid,
163
                    :version, 
164
                    :payload, 
165
                    :type,
166
                    :body, 
167
                    :occurred_on
168
            )';
169
170
        $stmt = $this->connection->prepare($sql);
171
        $stmt->bindParam(':uuid', $uuid);
172
        $stmt->bindParam(':version', $version);
173
        $stmt->bindParam(':payload', $payload);
174
        $stmt->bindParam(':type', $type);
175
        $stmt->bindParam(':body', $body);
176
        $stmt->bindParam(':occurred_on', $occurredOn);
177
        $stmt->execute();
178
    }
179
}
180