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