|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DDDominio\EventSourcing\Snapshotting\Vendor; |
|
4
|
|
|
|
|
5
|
|
|
use DDDominio\EventSourcing\EventStore\InitializableInterface; |
|
6
|
|
|
use DDDominio\EventSourcing\Serialization\SerializerInterface; |
|
7
|
|
|
use DDDominio\EventSourcing\Snapshotting\SnapshotInterface; |
|
8
|
|
|
use DDDominio\EventSourcing\Snapshotting\SnapshotStoreInterface; |
|
9
|
|
|
|
|
10
|
|
|
class MySqlJsonSnapshotStore implements SnapshotStoreInterface, InitializableInterface |
|
11
|
|
|
{ |
|
12
|
|
|
const SNAPSHOTS_TABLE = 'snapshots'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var \PDO |
|
16
|
|
|
*/ |
|
17
|
|
|
private $connection; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var SerializerInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $serializer; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param \PDO $connection |
|
26
|
|
|
* @param SerializerInterface $serializer |
|
27
|
|
|
*/ |
|
28
|
4 |
|
public function __construct( |
|
29
|
|
|
\PDO $connection, |
|
30
|
|
|
SerializerInterface $serializer |
|
31
|
|
|
) { |
|
32
|
4 |
|
$this->connection = $connection; |
|
33
|
4 |
|
$this->serializer = $serializer; |
|
34
|
4 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param SnapshotInterface $snapshot |
|
38
|
|
|
*/ |
|
39
|
4 |
|
public function addSnapshot($snapshot) |
|
40
|
|
|
{ |
|
41
|
4 |
|
$stmt = $this->connection |
|
42
|
4 |
|
->prepare('INSERT INTO snapshots (aggregate_type, aggregate_id, type, version, snapshot) VALUES (:aggregateType, :aggregateId, :type, :version, :snapshot)'); |
|
43
|
4 |
|
$stmt->bindValue(':aggregateType', $snapshot->aggregateClass()); |
|
44
|
4 |
|
$stmt->bindValue(':aggregateId', $snapshot->aggregateId()); |
|
45
|
4 |
|
$stmt->bindValue(':type', get_class($snapshot)); |
|
46
|
4 |
|
$stmt->bindValue(':version', $snapshot->version()); |
|
47
|
4 |
|
$stmt->bindValue(':snapshot', $this->serializer->serialize($snapshot)); |
|
48
|
4 |
|
$stmt->execute(); |
|
49
|
4 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $aggregateClass |
|
53
|
|
|
* @param string $aggregateId |
|
54
|
|
|
* @return SnapshotInterface|null |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function findLastSnapshot($aggregateClass, $aggregateId) |
|
57
|
|
|
{ |
|
58
|
2 |
|
$stmt = $this->connection |
|
59
|
2 |
|
->prepare('SELECT type, snapshot FROM snapshots WHERE aggregate_type = :aggregateType AND aggregate_id = :aggregateId ORDER BY id DESC LIMIT 1'); |
|
60
|
2 |
|
$stmt->bindValue(':aggregateType', $aggregateClass); |
|
61
|
2 |
|
$stmt->bindValue(':aggregateId', $aggregateId); |
|
62
|
2 |
|
$stmt->execute(); |
|
63
|
2 |
|
$snapshot = $stmt->fetch(); |
|
64
|
2 |
|
return $snapshot ? $this->serializer->deserialize($snapshot['snapshot'], $snapshot['type']) : null; |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $aggregateClass |
|
69
|
|
|
* @param string $aggregateId |
|
70
|
|
|
* @param int $version |
|
71
|
|
|
* @return SnapshotInterface|null |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function findNearestSnapshotToVersion($aggregateClass, $aggregateId, $version) |
|
74
|
|
|
{ |
|
75
|
2 |
|
$stmt = $this->connection |
|
76
|
2 |
|
->prepare('SELECT type, snapshot FROM snapshots WHERE aggregate_type = :aggregateType AND aggregate_id = :aggregateId AND version <= :version ORDER BY version DESC LIMIT 1'); |
|
77
|
2 |
|
$stmt->bindValue(':aggregateType', $aggregateClass); |
|
78
|
2 |
|
$stmt->bindValue(':aggregateId', $aggregateId); |
|
79
|
2 |
|
$stmt->bindValue(':version', $version); |
|
80
|
2 |
|
$stmt->execute(); |
|
81
|
2 |
|
$snapshot = $stmt->fetch(); |
|
82
|
2 |
|
return $snapshot ? $this->serializer->deserialize($snapshot['snapshot'], $snapshot['type']) : null; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
4 |
|
public function initialize() |
|
86
|
|
|
{ |
|
87
|
4 |
|
$this->connection->exec( |
|
88
|
4 |
|
'CREATE TABLE `'.self::SNAPSHOTS_TABLE.'` ( |
|
89
|
|
|
`id` int(11) NOT NULL AUTO_INCREMENT, |
|
90
|
|
|
`aggregate_type` varchar(255) NOT NULL, |
|
91
|
|
|
`aggregate_id` varchar(255) NOT NULL, |
|
92
|
|
|
`type` varchar(255) NOT NULL, |
|
93
|
|
|
`version` int(11) NOT NULL, |
|
94
|
|
|
`snapshot` json NOT NULL, |
|
95
|
|
|
PRIMARY KEY (`id`) |
|
96
|
4 |
|
)' |
|
97
|
|
|
); |
|
98
|
4 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
View Code Duplication |
public function initialized() |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
try { |
|
106
|
|
|
$result = $this->connection->query('SELECT 1 FROM `'.self::SNAPSHOTS_TABLE.'` LIMIT 1'); |
|
107
|
|
|
} catch (\Exception $e) { |
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
return $result !== false; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|