1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Modules\Posts\Domain\Logic; |
4
|
|
|
|
5
|
|
|
use App\Modules\Posts\Api\Command\CreatePostCommand; |
6
|
|
|
use App\Modules\Posts\Api\Command\DeletePostCommand; |
7
|
|
|
use App\Modules\Posts\Api\Command\UpdatePostCommand; |
8
|
|
|
use App\Modules\Posts\Api\Event\Inbound\CommentCreatedPostsIEvent; |
9
|
|
|
use App\Modules\Posts\Api\Event\Inbound\CommentsBaselinedPostsIEvent; |
10
|
|
|
use App\Modules\Posts\Domain\Dto\PostDto; |
11
|
|
|
use App\Modules\Posts\Domain\Repository\PostsFindingRepositoryInterface; |
12
|
|
|
use DusanKasan\Knapsack\Collection; |
13
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
14
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
15
|
|
|
use Symfony\Component\Uid\Ulid; |
16
|
|
|
|
17
|
|
|
class PostsValidator |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param PostsFindingRepositoryInterface $findingRepository |
21
|
|
|
*/ |
22
|
|
|
public function __construct( |
23
|
|
|
private PostsFindingRepositoryInterface $findingRepository |
24
|
|
|
) |
25
|
|
|
{ |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param CreatePostCommand $command |
30
|
|
|
*/ |
31
|
|
|
function preCreate(CreatePostCommand $command): void |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$this->validatePostBody($command->getBody()); |
34
|
|
|
$this->validatePostTitle($command->getTitle()); |
35
|
|
|
$this->validatePostTags($command->getTags()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param UpdatePostCommand $command |
40
|
|
|
* @return PostDto|null |
41
|
|
|
*/ |
42
|
|
|
public function preUpdate(UpdatePostCommand $command): ?PostDto |
43
|
|
|
{ |
44
|
|
|
$post = $this->validatePostExists($command->getId()); |
|
|
|
|
45
|
|
|
$this->validatePostBody($command->getBody()); |
46
|
|
|
$this->validatePostTitle($command->getTitle()); |
47
|
|
|
$this->validatePostTags($command->getTags()); |
48
|
|
|
return $post; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param DeletePostCommand $command |
53
|
|
|
* @return PostDto|null |
54
|
|
|
*/ |
55
|
|
|
public function preDelete(DeletePostCommand $command): ?PostDto |
56
|
|
|
{ |
57
|
|
|
return $this->validatePostExists($command->getId()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function preHandleCommentCreated(CommentCreatedPostsIEvent $event): void |
61
|
|
|
{ |
62
|
|
|
$this->validatePostExists($event->getPostId()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function preHandleCommentsBaselined(CommentsBaselinedPostsIEvent $event): void |
66
|
|
|
{ |
67
|
|
|
$this->validatePostExists($event->getPostId()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Ulid $id |
72
|
|
|
* @return PostDto|null |
73
|
|
|
*/ |
74
|
|
|
private function validatePostExists(Ulid $id): ?PostDto |
75
|
|
|
{ |
76
|
|
|
$post = $this->findingRepository->findPost($id); |
77
|
|
|
if ($post == null) { |
78
|
|
|
throw new NotFoundHttpException(); |
79
|
|
|
} |
80
|
|
|
return $post; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $body |
85
|
|
|
*/ |
86
|
|
|
private function validatePostBody(string $body): void |
87
|
|
|
{ |
88
|
|
|
if (trim($body) == '') { |
89
|
|
|
throw new BadRequestHttpException("Post Body cannot be empty"); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $title |
95
|
|
|
*/ |
96
|
|
|
private function validatePostTitle(string $title): void |
97
|
|
|
{ |
98
|
|
|
if (trim($title) == '') { |
99
|
|
|
throw new BadRequestHttpException("Post Title cannot be empty"); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array<string> $tags |
105
|
|
|
*/ |
106
|
|
|
private function validatePostTags(array $tags): void |
107
|
|
|
{ |
108
|
|
|
if (count(Collection::from($tags)->filter(function ($tag) { |
109
|
|
|
return trim($tag) == ''; |
110
|
|
|
})->toArray()) > 0) { |
111
|
|
|
throw new BadRequestHttpException("Post Tags cannot be blank"); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
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.