1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Modules\Comments\Domain\Logic; |
4
|
|
|
|
5
|
|
|
use App\Modules\Comments\Api\Event\Inbound\PostBaselinedCommentsIEvent; |
6
|
|
|
use App\Modules\Comments\Api\Event\Inbound\PostCreatedCommentsIEvent; |
7
|
|
|
use App\Modules\Comments\Api\Event\Inbound\PostDeletedCommentsIEvent; |
8
|
|
|
use App\Modules\Comments\Api\Event\Inbound\PostUpdatedCommentsIEvent; |
9
|
|
|
use App\Modules\Comments\Domain\Dto\CreateNewCommentsPostHeaderDto; |
10
|
|
|
use App\Modules\Comments\Domain\Dto\DeleteExistingCommentsPostHeaderDto; |
11
|
|
|
use App\Modules\Comments\Domain\Dto\UpdateExistingCommentsPostHeaderDto; |
12
|
|
|
use App\Modules\Comments\Domain\Repository\CommentsDeletionRepositoryInterface; |
13
|
|
|
use App\Modules\Comments\Domain\Repository\CommentsPostHeadersFindingRepositoryInterface; |
14
|
|
|
use App\Modules\Comments\Domain\Repository\CommentsPostsEventsHandlingRepositoryInterface; |
15
|
|
|
use App\Modules\Comments\Domain\Transactions\CommentsTransactionFactoryInterface; |
16
|
|
|
|
17
|
|
|
trait PostEventsHandler |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param CommentsTransactionFactoryInterface $transactionFactory |
21
|
|
|
* @param CommentsPostsEventsHandlingRepositoryInterface $postEventsCommentsRepository |
22
|
12 |
|
* @param CommentsDeletionRepositoryInterface $commentsDeletionRepository |
23
|
|
|
*/ |
24
|
|
|
public function __construct( |
25
|
|
|
private CommentsTransactionFactoryInterface $transactionFactory, |
26
|
|
|
private CommentsPostsEventsHandlingRepositoryInterface $postEventsCommentsRepository, |
27
|
|
|
private CommentsDeletionRepositoryInterface $commentsDeletionRepository, |
28
|
12 |
|
private CommentsPostHeadersFindingRepositoryInterface $postHeadersFindingRepository |
29
|
|
|
) |
30
|
|
|
{ |
31
|
|
|
} |
32
|
|
|
|
33
|
11 |
|
/** |
34
|
|
|
* @param PostBaselinedCommentsIEvent $event |
35
|
11 |
|
*/ |
36
|
11 |
|
public function onPostBaselined(PostBaselinedCommentsIEvent $event): void |
37
|
11 |
|
{ |
38
|
11 |
|
$this->transactionFactory->createTransaction(function () use ($event) { |
39
|
11 |
|
if ($this->postHeadersFindingRepository->postExists($event->getId())) { |
40
|
11 |
|
$this->postEventsCommentsRepository->updatePostHeader( |
41
|
11 |
|
new UpdateExistingCommentsPostHeaderDto( |
42
|
11 |
|
$event->getId(), |
43
|
11 |
|
$event->getTitle(), |
44
|
11 |
|
$event->getTags(), |
45
|
|
|
$event->getVersion() |
46
|
|
|
) |
47
|
11 |
|
); |
48
|
|
|
} else { |
49
|
11 |
|
$this->postEventsCommentsRepository->createPostHeader( |
50
|
|
|
new CreateNewCommentsPostHeaderDto( |
51
|
|
|
$event->getId(), |
52
|
|
|
$event->getTitle(), |
53
|
|
|
$event->getTags(), |
54
|
4 |
|
$event->getVersion() |
55
|
|
|
) |
56
|
4 |
|
); |
57
|
4 |
|
} |
58
|
4 |
|
|
59
|
4 |
|
})->execute(); |
60
|
4 |
|
|
61
|
4 |
|
} |
62
|
4 |
|
|
63
|
4 |
|
/** |
64
|
|
|
* @param PostCreatedCommentsIEvent $event |
65
|
|
|
*/ |
66
|
4 |
|
public function onPostCreated(PostCreatedCommentsIEvent $event): void |
67
|
|
|
{ |
68
|
4 |
|
$this->transactionFactory->createTransaction(function () use ($event) { |
69
|
|
|
$this->postEventsCommentsRepository->createPostHeader( |
70
|
|
|
new CreateNewCommentsPostHeaderDto( |
71
|
|
|
$event->getId(), |
72
|
|
|
$event->getTitle(), |
73
|
2 |
|
$event->getTags() |
74
|
|
|
) |
75
|
2 |
|
); |
76
|
2 |
|
})->execute(); |
77
|
2 |
|
|
78
|
2 |
|
} |
79
|
|
|
|
80
|
2 |
|
/** |
81
|
|
|
* @param PostUpdatedCommentsIEvent $event |
82
|
2 |
|
*/ |
83
|
1 |
|
public function onPostUpdated(PostUpdatedCommentsIEvent $event): void |
84
|
|
|
{ |
85
|
|
|
$this->transactionFactory->createTransaction(function () use ($event) { |
86
|
|
|
$this->postEventsCommentsRepository->updatePostHeader( |
87
|
|
|
new UpdateExistingCommentsPostHeaderDto( |
88
|
|
|
$event->getId(), |
89
|
|
|
$event->getTitle(), |
90
|
|
|
$event->getTags(), |
91
|
|
|
$event->getLastVersion() |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
})->execute(); |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param PostDeletedCommentsIEvent $event |
100
|
|
|
*/ |
101
|
|
|
public function onPostDeleted(PostDeletedCommentsIEvent $event): void |
102
|
|
|
{ |
103
|
|
|
$this->transactionFactory->createTransaction(function () use ($event) { |
104
|
|
|
$this->commentsDeletionRepository->deleteCommentsForPost($event->getId()); |
105
|
|
|
$this->postEventsCommentsRepository->deletePostHeader( |
106
|
|
|
new DeleteExistingCommentsPostHeaderDto($event->getId()) |
107
|
|
|
); |
108
|
|
|
})->execute(); |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
} |