CommentsEventsHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 47
ccs 24
cts 24
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onCommentsBaselined() 0 13 1
A onCommentCreated() 0 13 1
A __construct() 0 6 1
1
<?php
2
3
namespace App\Modules\Posts\Domain\Logic;
4
5
use App\Modules\Posts\Api\Event\Inbound\CommentCreatedPostsIEvent;
6
use App\Modules\Posts\Api\Event\Inbound\CommentsBaselinedPostsIEvent;
7
use App\Modules\Posts\Domain\Dto\UpdatePostCommentsDto;
8
use App\Modules\Posts\Domain\Repository\PostsCommentsEventHandlingRepositoryInterface;
9
use App\Modules\Posts\Domain\Transactions\PostTransactionFactoryInterface;
10
11
trait CommentsEventsHandler
12
{
13
14
    /**
15
     * @param PostTransactionFactoryInterface $transactionFactory
16
     * @param PostsCommentsEventHandlingRepositoryInterface $commentsEventHandlingRepository
17
     * @param PostsValidator $postValidator
18
     */
19 24
    public function __construct(
20
        private PostTransactionFactoryInterface               $transactionFactory,
21
        private PostsCommentsEventHandlingRepositoryInterface $commentsEventHandlingRepository,
22
        private PostsValidator                                $postValidator
23
    )
24
    {
25 24
    }
26
27
    /**
28
     * @param CommentCreatedPostsIEvent $event
29
     */
30 2
   public function onCommentCreated(CommentCreatedPostsIEvent $event): void
31
    {
32 2
        $this->postValidator->preHandleCommentCreated($event);
33 2
        $this->transactionFactory
34 2
            ->createTransaction(function () use ($event) {
35 2
                $this->commentsEventHandlingRepository->updateAllComments(
36 2
                    new UpdatePostCommentsDto(
37 2
                        $event->getPostId(),
38 2
                        $event->getComments()
39
                    )
40
                );
41 2
            })
42 2
            ->execute();
43 2
    }
44
45 1
   public function onCommentsBaselined(CommentsBaselinedPostsIEvent $event): void
46
    {
47 1
        $this->postValidator->preHandleCommentsBaselined($event);
48 1
        $this->transactionFactory
49 1
            ->createTransaction(function () use ($event) {
50 1
                $this->commentsEventHandlingRepository->updateAllComments(
51 1
                    new UpdatePostCommentsDto(
52 1
                        $event->getPostId(),
53 1
                        $event->getComments()
54 1
                    ), false
55
                );
56 1
            })
57 1
            ->execute();
58
    }
59
}