Test Setup Failed
Push — main ( abdcb9...2e5f68 )
by Slawomir
04:37
created

PostsValidator::preHandleCommentCreated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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());
0 ignored issues
show
Bug introduced by
It seems like $command->getId() can also be of type null; however, parameter $id of App\Modules\Posts\Domain...r::validatePostExists() does only seem to accept Symfony\Component\Uid\Ulid, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        $post = $this->validatePostExists(/** @scrutinizer ignore-type */ $command->getId());
Loading history...
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
}