Passed
Push — main ( 58e8d0...65c0e3 )
by Slawomir
04:52
created

DoctrineCommentsPostHeadersFindingRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 34
ccs 12
cts 14
cp 0.8571
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A findPostHeaders() 0 13 3
A postExists() 0 9 1
1
<?php
2
3
namespace App\Modules\Comments\Persistence\Doctrine\Repository;
4
5
use App\Modules\Comments\Domain\Dto\CommentsPostHeaderDto;
6
use App\Modules\Comments\Domain\Repository\CommentsPostHeadersFindingRepositoryInterface;
7
use App\Modules\Comments\Persistence\Doctrine\Entity\CommentPostHeader;
8
use Symfony\Component\Uid\Ulid;
9
10
class DoctrineCommentsPostHeadersFindingRepository extends DoctrineCommentsRepository implements CommentsPostHeadersFindingRepositoryInterface
11
{
12
13
    /**
14
     * @return array<CommentsPostHeaderDto>
15
     */
16 2
    public function findPostHeaders(?\DateTime $from = null): array
17
    {
18 2
        $headerClass = CommentPostHeader::class;
19 2
        $dtoClass = CommentsPostHeaderDto::class;
20 2
        $dql = "select new $dtoClass(p.id, p.title, p.summary, p.tags, p.createdById, p.createdByName, p.createdAt, p.version) from $headerClass p";
21 2
        if ($from != null) {
22
            $dql = $dql . " where p.createdAt >= :from";
23
        }
24 2
        $query = $this->getEntityManager()->createQuery($dql);
25 2
        if ($from != null) {
26
            $query = $query->setParameter("from", $from);
27
        }
28 2
        return $query->getArrayResult();
29
    }
30
31
    /**
32
     * @param Ulid $postId
33
     * @return bool
34
     */
35
    public function postExists(Ulid $postId): bool
36
    {
37 2
        $headerClass = CommentPostHeader::class;
38
        $query = $this->getEntityManager()->createQuery(
39 2
            "select count(p.id) as count from $headerClass p where p.id = :id"
40 2
        );
41 2
        return $query
42
                ->setParameter("id", $postId, "ulid")
43
                ->getResult()[0]["count"] > 0;
44
    }
45
}