|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Modules\Posts\Domain\Logic; |
|
4
|
|
|
|
|
5
|
|
|
use App\Infrastructure\Events\Api\ApplicationEventPublisherInterface; |
|
6
|
|
|
use App\Infrastructure\Security\LoggedInUser; |
|
7
|
|
|
use App\Infrastructure\Security\LoggedInUserProviderInterface; |
|
8
|
|
|
use App\Infrastructure\Utils\StringUtil; |
|
9
|
|
|
use App\Modules\Posts\Api\Command\CreatePostCommand; |
|
10
|
|
|
use App\Modules\Posts\Api\Command\Response\CreatePostCommandResponse; |
|
11
|
|
|
use App\Modules\Posts\Domain\Dto\CreateNewPostDto; |
|
12
|
|
|
use App\Modules\Posts\Domain\Event\Outbound\PostCreatedOEvent; |
|
13
|
|
|
use App\Modules\Posts\Domain\Repository\PostsCreationRepositoryInterface; |
|
14
|
|
|
use App\Modules\Posts\Domain\Transactions\PostTransactionFactoryInterface; |
|
15
|
|
|
|
|
16
|
|
|
trait PostsCreator |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param ApplicationEventPublisherInterface $publisher |
|
20
|
|
|
* @param PostsCreationRepositoryInterface $creationRepository |
|
21
|
|
|
* @param LoggedInUserProviderInterface $loggedInUserProvider |
|
22
|
|
|
* @param PostTransactionFactoryInterface $transactionFactory |
|
23
|
|
|
* @param PostsValidator $validator |
|
24
|
|
|
*/ |
|
25
|
24 |
|
public function __construct( |
|
26
|
|
|
private ApplicationEventPublisherInterface $publisher, |
|
27
|
|
|
private PostsCreationRepositoryInterface $creationRepository, |
|
28
|
|
|
private LoggedInUserProviderInterface $loggedInUserProvider, |
|
29
|
|
|
private PostTransactionFactoryInterface $transactionFactory, |
|
30
|
|
|
private PostsValidator $validator |
|
31
|
|
|
) |
|
32
|
|
|
{ |
|
33
|
24 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param CreatePostCommand $command |
|
37
|
|
|
* @return CreatePostCommandResponse |
|
38
|
|
|
*/ |
|
39
|
22 |
|
public function createPost(CreatePostCommand $command): CreatePostCommandResponse |
|
40
|
|
|
{ |
|
41
|
22 |
|
$this->validator->preCreate($command); |
|
42
|
19 |
|
$newPost = $this->fromCreateCommand($command); |
|
43
|
19 |
|
$id = $this->transactionFactory |
|
44
|
19 |
|
->createTransaction(function () use ($newPost) { |
|
45
|
19 |
|
return $this->creationRepository->createPost($newPost); |
|
46
|
19 |
|
}) |
|
47
|
19 |
|
->afterCommit(function ($id) use ($newPost) { |
|
48
|
19 |
|
$this->publisher->publish(new PostCreatedOEvent($id, $newPost)); |
|
49
|
19 |
|
}) |
|
50
|
19 |
|
->execute(); |
|
51
|
|
|
|
|
52
|
19 |
|
return new CreatePostCommandResponse($id); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param CreatePostCommand $command |
|
57
|
|
|
* @return CreateNewPostDto |
|
58
|
|
|
*/ |
|
59
|
19 |
|
private function fromCreateCommand(CreatePostCommand $command): CreateNewPostDto |
|
60
|
|
|
{ |
|
61
|
19 |
|
return new CreateNewPostDto( |
|
62
|
19 |
|
$command->getTitle(), |
|
63
|
19 |
|
$command->getBody(), |
|
64
|
19 |
|
StringUtil::getSummary($command->getBody()), |
|
65
|
19 |
|
$command->getTags(), |
|
66
|
19 |
|
$this->getUser()->getId(), |
|
67
|
19 |
|
$this->getUser()->getEmail(), |
|
68
|
19 |
|
new \DateTime() |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return LoggedInUser |
|
74
|
|
|
*/ |
|
75
|
19 |
|
private function getUser(): LoggedInUser |
|
76
|
|
|
{ |
|
77
|
19 |
|
return $this->loggedInUserProvider->getUser(); |
|
78
|
|
|
} |
|
79
|
|
|
} |