|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Tests\Modules\Comments\Unit\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use App\Infrastructure\Pagination\Page; |
|
6
|
|
|
use App\Modules\Comments\Domain\Dto\CommentDto; |
|
7
|
|
|
use App\Modules\Comments\Domain\Dto\CommentsPostHeaderDto; |
|
8
|
|
|
use App\Modules\Comments\Domain\Dto\CommentWithPostDto; |
|
9
|
|
|
use App\Modules\Comments\Domain\Dto\CreateNewCommentDto; |
|
10
|
|
|
use App\Modules\Comments\Domain\Dto\CreateNewCommentsPostHeaderDto; |
|
11
|
|
|
use App\Modules\Comments\Domain\Dto\DeleteExistingCommentsPostHeaderDto; |
|
12
|
|
|
use App\Modules\Comments\Domain\Dto\UpdatedCommentsPostHeadersUserNameDto; |
|
13
|
|
|
use App\Modules\Comments\Domain\Dto\UpdateExistingCommentsPostHeaderDto; |
|
14
|
|
|
use App\Modules\Comments\Domain\Repository\CommentsCreationRepositoryInterface; |
|
15
|
|
|
use App\Modules\Comments\Domain\Repository\CommentsDeletionRepositoryInterface; |
|
16
|
|
|
use App\Modules\Comments\Domain\Repository\CommentsFindingRepositoryInterface; |
|
17
|
|
|
use App\Modules\Comments\Domain\Repository\CommentsPostsEventsHandlingRepositoryInterface; |
|
18
|
|
|
use App\Modules\Comments\Domain\Repository\CommentsPostHeadersFindingRepositoryInterface; |
|
19
|
|
|
use App\Modules\Comments\Domain\Repository\CommentsSecurityEventsHandlingRepositoryInterface; |
|
20
|
|
|
use DusanKasan\Knapsack\Collection; |
|
21
|
|
|
use Symfony\Component\Uid\Ulid; |
|
22
|
|
|
|
|
23
|
|
|
class InMemoryCommentsRepository implements |
|
24
|
|
|
CommentsPostsEventsHandlingRepositoryInterface, |
|
25
|
|
|
CommentsPostHeadersFindingRepositoryInterface, |
|
26
|
|
|
CommentsCreationRepositoryInterface, |
|
27
|
|
|
CommentsFindingRepositoryInterface, |
|
28
|
|
|
CommentsDeletionRepositoryInterface, |
|
29
|
|
|
CommentsSecurityEventsHandlingRepositoryInterface |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
private static Collection $postHeaders; |
|
33
|
|
|
private static Collection $comments; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct() |
|
36
|
|
|
{ |
|
37
|
|
|
self::clear(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public static function clear(): void |
|
41
|
|
|
{ |
|
42
|
|
|
self::$postHeaders = Collection::from([]); |
|
43
|
|
|
self::$comments = Collection::from([]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
function createPostHeader(CreateNewCommentsPostHeaderDto $newPostHeader): void |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
self::$postHeaders = self::$postHeaders->append(new InMemoryCommentPostHeader( |
|
49
|
|
|
$newPostHeader->getId(), |
|
50
|
|
|
$newPostHeader->getTitle(), |
|
51
|
|
|
$newPostHeader->getSummary(), |
|
52
|
|
|
$newPostHeader->getTags(), |
|
53
|
|
|
$newPostHeader->getCreatedById(), |
|
54
|
|
|
$newPostHeader->getCreatedByName(), |
|
55
|
|
|
$newPostHeader->getCreatedAt(), |
|
56
|
|
|
$newPostHeader->getVersion() |
|
57
|
|
|
)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
function updatePostHeader(UpdateExistingCommentsPostHeaderDto $updatedPostHeader): void |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
foreach (self::$postHeaders |
|
63
|
|
|
->filter(function ($header) use ($updatedPostHeader) { |
|
64
|
|
|
return $header->getId() == $updatedPostHeader->getId() && $header->getVersion() <= $updatedPostHeader->getVersion(); |
|
65
|
|
|
}) |
|
66
|
|
|
->toArray() as $header) { |
|
67
|
|
|
$header->setTitle($updatedPostHeader->getTitle()); |
|
68
|
|
|
$header->setSummary($updatedPostHeader->getSummary()); |
|
69
|
|
|
$header->setTags($updatedPostHeader->getTags()); |
|
70
|
|
|
$header->setVersion($updatedPostHeader->getVersion()); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
function deletePostHeader(DeleteExistingCommentsPostHeaderDto $deletedPostHeader): void |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
self::$postHeaders = self::$postHeaders->filter( |
|
77
|
|
|
function ($header) use ($deletedPostHeader) { |
|
78
|
|
|
return $header->getId() != $deletedPostHeader->getId(); |
|
79
|
|
|
} |
|
80
|
|
|
)->realize(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
function findPostHeaders(?\DateTime $from = null): array |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
return self::$postHeaders |
|
86
|
|
|
->filter(function ($header) use ($from) { |
|
87
|
|
|
return $from == null || $header->getCreatedAt() >= $from; |
|
88
|
|
|
}) |
|
89
|
|
|
->map(function ($header) { |
|
90
|
|
|
return new CommentsPostHeaderDto( |
|
91
|
|
|
$header->getId(), |
|
92
|
|
|
$header->getTitle(), |
|
93
|
|
|
$header->getSummary(), |
|
94
|
|
|
$header->getTags(), |
|
95
|
|
|
$header->getCreatedById(), |
|
96
|
|
|
$header->getCreatedByName(), |
|
97
|
|
|
$header->getCreatedAt(), |
|
98
|
|
|
$header->getVersion() |
|
99
|
|
|
); |
|
100
|
|
|
}) |
|
101
|
|
|
->toArray(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function createComment(CreateNewCommentDto $newComment): Ulid |
|
105
|
|
|
{ |
|
106
|
|
|
$id = new Ulid(); |
|
107
|
|
|
$parentComment = null; |
|
108
|
|
|
if ($newComment->getParentId() != null) { |
|
109
|
|
|
$parentComment = self::$comments->find(function ($comment) use ($newComment) { |
|
|
|
|
|
|
110
|
|
|
return $comment->getId() == $newComment->getParentId(); |
|
111
|
|
|
}); |
|
112
|
|
|
} |
|
113
|
|
|
$post = self::$postHeaders->find(function ($post) use ($newComment) { |
|
|
|
|
|
|
114
|
|
|
return $post->getId() == $newComment->getPostId(); |
|
115
|
|
|
}); |
|
116
|
|
|
$comment = new InMemoryComment( |
|
117
|
|
|
$id, |
|
118
|
|
|
$newComment->getAuthor(), |
|
119
|
|
|
$newComment->getBody(), |
|
120
|
|
|
$newComment->getCreatedAt(), |
|
121
|
|
|
$parentComment, |
|
122
|
|
|
$post |
|
123
|
|
|
); |
|
124
|
|
|
self::$comments = self::$comments->append($comment); |
|
125
|
|
|
$data = $post->getComments(); |
|
126
|
|
|
array_push($data, $comment); |
|
127
|
|
|
$post->setComments($data); |
|
128
|
|
|
return $id; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function getCommentsCount(Ulid $postId): int |
|
132
|
|
|
{ |
|
133
|
|
|
return self::$postHeaders |
|
|
|
|
|
|
134
|
|
|
->filter(function ($post) use ($postId) { |
|
135
|
|
|
return $post->getId() == $postId; |
|
136
|
|
|
}) |
|
137
|
|
|
->map(function ($post) { |
|
138
|
|
|
return count($post->getComments()); |
|
139
|
|
|
}) |
|
140
|
|
|
->first(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function findCommentsByPostId(Ulid $postId): array |
|
144
|
|
|
{ |
|
145
|
|
|
$post = self::$postHeaders |
|
|
|
|
|
|
146
|
|
|
->find(function ($post) use ($postId) { |
|
147
|
|
|
return $post->getId() == $postId; |
|
148
|
|
|
}); |
|
149
|
|
|
if ($post == null) { |
|
|
|
|
|
|
150
|
|
|
return []; |
|
151
|
|
|
} |
|
152
|
|
|
return Collection::from($post->getComments()) |
|
153
|
|
|
->map(function ($comment) { |
|
154
|
|
|
$parentId = $comment->getParentComment() != null ? $comment->getParentComment()->getId() : null; |
|
155
|
|
|
return new CommentDto( |
|
156
|
|
|
$comment->getId(), |
|
157
|
|
|
$comment->getAuthor(), |
|
158
|
|
|
$comment->getBody(), |
|
159
|
|
|
$parentId, |
|
160
|
|
|
$comment->getCreatedAt() |
|
161
|
|
|
); |
|
162
|
|
|
})->toArray(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function findLatestComments(int $pageNo): Page |
|
166
|
|
|
{ |
|
167
|
|
|
$from = ($pageNo - 1) * self::PAGE_SIZE; |
|
168
|
|
|
$to = $from + self::PAGE_SIZE; |
|
169
|
|
|
$data = self::$comments |
|
170
|
|
|
->map(function ($comment) { |
|
171
|
|
|
$parentId = $comment->getParentComment() != null ? $comment->getParentComment()->getId() : null; |
|
172
|
|
|
return new CommentWithPostDto( |
|
173
|
|
|
$comment->getId(), |
|
174
|
|
|
$comment->getAuthor(), |
|
175
|
|
|
$comment->getBody(), |
|
176
|
|
|
$parentId, |
|
177
|
|
|
$comment->getCreatedAt(), |
|
178
|
|
|
$comment->getPost()->getId(), |
|
179
|
|
|
$comment->getPost()->getTitle(), |
|
180
|
|
|
$comment->getPost()->getSummary(), |
|
181
|
|
|
self::$postHeaders |
|
182
|
|
|
->filter(function ($post) use ($comment) { |
|
183
|
|
|
return $post->getId() == $comment->getPost()->getId(); |
|
184
|
|
|
})->size(), |
|
185
|
|
|
$comment->getPost()->getTags(), |
|
186
|
|
|
|
|
187
|
|
|
); |
|
188
|
|
|
}) |
|
189
|
|
|
->sort(function ($c1, $c2) { |
|
190
|
|
|
return $c1->getCreatedAt() < $c2->getCreatedAt() ? 1 : -1; |
|
191
|
|
|
}) |
|
192
|
|
|
->slice($from, $to) |
|
193
|
|
|
->toArray(); |
|
194
|
|
|
return new Page(array_values($data), self::$comments->size(), $pageNo, self::PAGE_SIZE); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function commentExists(Ulid $commentId): bool |
|
198
|
|
|
{ |
|
199
|
|
|
return self::$comments |
|
200
|
|
|
->filter(function ($comment) use ($commentId) { |
|
201
|
|
|
return $comment->getId() == $commentId; |
|
202
|
|
|
})->sizeIsGreaterThan(0); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function postExists(Ulid $postId): bool |
|
206
|
|
|
{ |
|
207
|
|
|
return self::$postHeaders |
|
208
|
|
|
->filter(function ($post) use ($postId) { |
|
209
|
|
|
return $post->getId() == $postId; |
|
210
|
|
|
})->sizeIsGreaterThan(0); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public function deleteCommentsForPost(Ulid $postId) |
|
214
|
|
|
{ |
|
215
|
|
|
self::$comments = self::$comments |
|
216
|
|
|
->filter(function ($comment) use ($postId) { |
|
217
|
|
|
return $comment->getPost()->getId() != $postId; |
|
218
|
|
|
}) |
|
219
|
|
|
->realize(); |
|
220
|
|
|
$post = self::$postHeaders->find(function ($post) use ($postId) { |
|
|
|
|
|
|
221
|
|
|
$post->getId() == $postId; |
|
222
|
|
|
}); |
|
223
|
|
|
if ($post != null) { |
|
|
|
|
|
|
224
|
|
|
$post->setComments([]); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
public function updateUserName(UpdatedCommentsPostHeadersUserNameDto $updatedUserName): void |
|
230
|
|
|
{ |
|
231
|
|
|
self::$postHeaders |
|
232
|
|
|
->filter(function ($post) use ($updatedUserName) { |
|
233
|
|
|
return $post->getCreatedByName() == $updatedUserName->getOldUserName(); |
|
234
|
|
|
}) |
|
235
|
|
|
->each(function ($post) use ($updatedUserName) { |
|
236
|
|
|
$post->setCreatedByName($updatedUserName->getNewUserName()); |
|
237
|
|
|
}) |
|
238
|
|
|
->realize(); |
|
239
|
|
|
} |
|
240
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.