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

findPostHeaders()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
ccs 8
cts 10
cp 0.8
crap 3.072
rs 9.9666
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
}