1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Modules\Posts\Domain\Logic; |
4
|
|
|
|
5
|
|
|
use App\Infrastructure\Events\Api\ApplicationEventPublisherInterface; |
6
|
|
|
use App\Modules\Posts\Api\Command\DeletePostCommand; |
7
|
|
|
use App\Modules\Posts\Domain\Dto\DeleteExistingPostDto; |
8
|
|
|
use App\Modules\Posts\Domain\Event\Outbound\PostDeletedOEvent; |
9
|
|
|
use App\Modules\Posts\Domain\Repository\PostsDeletionRepositoryInterface; |
10
|
|
|
use App\Modules\Posts\Domain\Transactions\PostTransactionFactoryInterface; |
11
|
|
|
|
12
|
|
|
trait PostsRemover |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param ApplicationEventPublisherInterface $eventPublisher |
16
|
|
|
* @param PostsDeletionRepositoryInterface $deletionRepository |
17
|
|
|
* @param PostTransactionFactoryInterface $transactionFactory |
18
|
|
|
* @param PostsValidator $validator |
19
|
|
|
*/ |
20
|
24 |
|
public function __construct( |
21
|
|
|
private ApplicationEventPublisherInterface $eventPublisher, |
22
|
|
|
private PostsDeletionRepositoryInterface $deletionRepository, |
23
|
|
|
private PostTransactionFactoryInterface $transactionFactory, |
24
|
|
|
private PostsValidator $validator |
25
|
|
|
) |
26
|
|
|
{ |
27
|
24 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param DeletePostCommand $command |
31
|
|
|
*/ |
32
|
3 |
|
public function deletePost(DeletePostCommand $command): void |
33
|
|
|
{ |
34
|
3 |
|
$this->validator->preDelete($command); |
35
|
2 |
|
$deletedPost = $this->fromDeleteCommand($command); |
36
|
2 |
|
$this->transactionFactory |
37
|
2 |
|
->createTransaction(function () use ($deletedPost) { |
38
|
2 |
|
$this->deletionRepository->deletePost($deletedPost); |
39
|
2 |
|
}) |
40
|
2 |
|
->afterCommit(function () use ($deletedPost) { |
41
|
2 |
|
$this->publisher->publish(new PostDeletedOEvent($deletedPost)); |
42
|
2 |
|
}) |
43
|
2 |
|
->execute(); |
44
|
2 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param DeletePostCommand $command |
48
|
|
|
* @return DeleteExistingPostDto |
49
|
|
|
*/ |
50
|
2 |
|
private function fromDeleteCommand(DeletePostCommand $command): DeleteExistingPostDto |
51
|
|
|
{ |
52
|
2 |
|
return new DeleteExistingPostDto($command->getId()); |
53
|
|
|
} |
54
|
|
|
} |