|
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 Predis\Client; |
|
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
|
|
|
|
|
19
|
|
|
class RedisEventStoreRepository implements EventStoreRepositoryInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Client |
|
23
|
|
|
*/ |
|
24
|
|
|
private $client; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
private $return; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* RedisEventRepository constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Client $client |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Client $client, $return = self::RETURN_AS_ARRAY) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->client = $client; |
|
39
|
|
|
$this->return = $return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param AggregateUuid $uuid |
|
44
|
|
|
* @param int $returnType |
|
45
|
|
|
* @return array|null |
|
46
|
|
|
*/ |
|
47
|
|
|
public function byUuid(AggregateUuid $uuid, $returnType = self::RETURN_AS_ARRAY) |
|
48
|
|
|
{ |
|
49
|
|
|
$events = array_map(function($event){ |
|
50
|
|
|
return unserialize($event); |
|
51
|
|
|
}, $this->client->hgetall((string) $uuid)); |
|
52
|
|
|
|
|
53
|
|
|
if (!empty($events)) { |
|
54
|
|
|
ksort($events); |
|
55
|
|
|
|
|
56
|
|
|
return $this->buildEventAggregate($events, $returnType); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param array $events |
|
64
|
|
|
* @param $returnType |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
private function buildEventAggregate(array $events, $returnType) |
|
69
|
|
|
{ |
|
70
|
|
|
if ($returnType === self::RETURN_AS_ARRAY) { |
|
71
|
|
|
return $this->buildEventAggregateAsArray($events); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->buildEventAggregateAsObject($events); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param array $events |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
View Code Duplication |
private function buildEventAggregateAsArray(array $events) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$returnArray = []; |
|
85
|
|
|
|
|
86
|
|
|
/** @var Event $event */ |
|
87
|
|
|
foreach ($events as $event) { |
|
88
|
|
|
$returnArray[] = [ |
|
89
|
|
|
'uuid' => (string) $event->uuid(), |
|
90
|
|
|
'version' => $event->version(), |
|
91
|
|
|
'payload' => $event->payload(), |
|
92
|
|
|
'type' => $event->type(), |
|
93
|
|
|
'body' => $event->body(), |
|
94
|
|
|
'occurred_on' => $event->occurredOn()->format('Y-m-d H:i:s.u') |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $returnArray; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param array $events |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
private function buildEventAggregateAsObject(array $events) |
|
107
|
|
|
{ |
|
108
|
|
|
$returnObject = []; |
|
109
|
|
|
|
|
110
|
|
|
/** @var Event $event */ |
|
111
|
|
|
foreach ($events as $event) { |
|
112
|
|
|
$returnObject[] = $event; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $returnObject; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param AggregateUuid $uuid |
|
120
|
|
|
* |
|
121
|
|
|
* @return int |
|
122
|
|
|
*/ |
|
123
|
|
|
public function count(AggregateUuid $uuid) |
|
124
|
|
|
{ |
|
125
|
|
|
return count($this->client->hgetall((string) $uuid)); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param EventInterface $event |
|
130
|
|
|
*/ |
|
131
|
|
|
public function save(EventInterface $event) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->client->hset( |
|
134
|
|
|
(string) $event->uuid(), |
|
135
|
|
|
$this->count($event->uuid()), |
|
136
|
|
|
serialize( |
|
137
|
|
|
new $event( |
|
138
|
|
|
$event->uuid(), |
|
|
|
|
|
|
139
|
|
|
$event->type(), |
|
140
|
|
|
$event->body(), |
|
141
|
|
|
$this->count($event->uuid()), |
|
142
|
|
|
$event->occurredOn()->format('Y-m-d H:i:s.u') |
|
143
|
|
|
) |
|
144
|
|
|
) |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
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.