Code Duplication    Length = 160-160 lines in 2 locations

src/Infrastructure/Persistence/DbalEventStoreRepository.php 1 location

@@ 20-179 (lines=160) @@
17
use SimpleEventStoreManager\Domain\Model\AggregateUuid;
18
use SimpleEventStoreManager\Infrastructure\Drivers\PdoDriver;
19
20
class DbalEventStoreRepository implements EventStoreRepositoryInterface
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

src/Infrastructure/Persistence/PdoEventStoreRepository.php 1 location

@@ 19-178 (lines=160) @@
16
use SimpleEventStoreManager\Domain\Model\AggregateUuid;
17
use SimpleEventStoreManager\Infrastructure\Drivers\PdoDriver;
18
19
class PdoEventStoreRepository implements EventStoreRepositoryInterface
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