CommentsFinder   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 56
ccs 32
cts 32
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findCommentsForPost() 0 14 1
A findLatestComments() 0 20 1
1
<?php
2
3
namespace App\Modules\Comments\Domain\Logic;
4
5
use App\Infrastructure\Pagination\Page;
6
use App\Modules\Comments\Api\Query\FindCommentsByPostIdQuery;
7
use App\Modules\Comments\Api\Query\FindLatestCommentsQuery;
8
use App\Modules\Comments\Api\Query\Response\FindCommentsByPostIdQueryResponse;
9
use App\Modules\Comments\Api\Query\Response\FindLatestCommentsQueryResponse;
10
use App\Modules\Comments\Domain\Repository\CommentsFindingRepositoryInterface;
11
use DusanKasan\Knapsack\Collection;
12
13
trait CommentsFinder
14
{
15
    /**
16
     * @param CommentsFindingRepositoryInterface $commentsFindingRepository
17
     */
18 12
    public function __construct(
19
        private CommentsFindingRepositoryInterface $commentsFindingRepository
20
    )
21
    {
22 12
    }
23
24
25
    /**
26
     * @param FindCommentsByPostIdQuery $query
27
     * @return array<FindCommentsByPostIdQueryResponse>
28
     */
29 4
    public function findCommentsForPost(FindCommentsByPostIdQuery $query): array
30
    {
31 4
        return Collection::from(
32 4
            $this->commentsFindingRepository->findCommentsByPostId($query->getPostId())
33 4
        )->map(function ($comment) {
34 4
            return new FindCommentsByPostIdQueryResponse(
35 4
                $comment->getId(),
36 4
                $comment->getAuthor(),
37 4
                $comment->getBody(),
38 4
                $comment->getParentId(),
39 4
                $comment->getCreatedAt(),
40
            );
41 4
        })
42 4
            ->toArray();
43
    }
44
45
    /**
46
     * @param FindLatestCommentsQuery $query
47
     * @return Page<FindLatestCommentsQueryResponse>
48
     */
49 2
    public function findLatestComments(FindLatestCommentsQuery $query): Page
50
    {
51 2
        $result = $this->commentsFindingRepository->findLatestComments($query->getPageNo());
52 2
        $data = Collection::from(
53 2
            $result->getData()
54 2
        )->map(function ($comment) {
55 2
            return new FindLatestCommentsQueryResponse(
56 2
                $comment->getId(),
57 2
                $comment->getAuthor(),
58 2
                $comment->getBody(),
59 2
                $comment->getParentId(),
60 2
                $comment->getCreatedAt(),
61 2
                $comment->getPostId(),
62 2
                $comment->getPostTitle(),
63 2
                $comment->getPostTags()
64 2
            );
65 2
        })
66
            ->toArray();
67 2
68 2
        return new Page($data, $result->getCount(), $result->getPageNo(), $result->getPageSize());
69
    }
70
}