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

DoctrineUserPostHeadersFindingRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 54
ccs 21
cts 28
cp 0.75
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findPostHeaders() 0 9 1
A findPostsByUserId() 0 20 1
A getCount() 0 7 1
1
<?php
2
3
namespace App\Modules\Security\Persistence\Doctrine\Repository;
4
5
use App\Infrastructure\Pagination\Page;
6
use App\Modules\Security\Domain\Dto\UserPostHeaderDto;
7
use App\Modules\Security\Domain\Repository\UserPostHeadersFindingRepositoryInterface;
8
use App\Modules\Security\Persistence\Doctrine\Entity\UserPostHeader;
9
use Symfony\Component\Uid\Ulid;
10
11
class DoctrineUserPostHeadersFindingRepository extends DoctrineSecurityRepository implements UserPostHeadersFindingRepositoryInterface
12
{
13
14
    /**
15
     * @return array<UserPostHeaderDto>
16
     */
17
   public function findPostHeaders(): array
18
    {
19
        $headerClass = UserPostHeader::class;
20
        $dtoClass = UserPostHeaderDto::class;
21
        $query = $this->getEntityManager()->createQuery(
22
            "select new $dtoClass(p.id, p.title, p.summary, p.tags, u.id, u.email, p.createdAt, p.version, p.commentsCount) 
23
                from $headerClass p join p.user u order by p.createdAt asc"
24
        );
25
        return $query->getArrayResult();
26
    }
27
28
    /**
29
     * @return Page<UserPostHeaderDto>
30
     */
31 1
    public function findPostsByUserId(Ulid $userId, int $pageNo): Page
32
    {
33 1
        $headerClass = UserPostHeader::class;
34 1
        $dtoClass = UserPostHeaderDto::class;
35 1
        $query = $this->getEntityManager()->createQuery(
36 1
            "select new $dtoClass(p.id, p.title, p.summary, p.tags, u.id, u.email, p.createdAt, p.version, p.commentsCount) 
37 1
                from $headerClass p join p.user u
38
                where u.id = :userId
39
                order by p.id desc"
40
        );
41 1
        $data = $query
42 1
            ->setParameter("userId", $userId, "ulid")
43 1
            ->setFirstResult(($pageNo - 1) * self::PAGE_SIZE)
44 1
            ->setMaxResults(self::PAGE_SIZE)
45 1
            ->getArrayResult();
46 1
        return new Page(
47 1
            $data,
48 1
            $this->getCount($userId),
49
            $pageNo,
50 1
            self::PAGE_SIZE
51
        );
52
    }
53
54 1
    /**
55
     * @param $userId
56 1
     * @return int
57 1
     */
58 1
    private function getCount($userId): int
59
    {
60 1
        $postClass = UserPostHeader::class;
61
        $query = $this->getEntityManager()->createQuery(
62 1
            "select count(p.id) as count from $postClass p join p.user u where u.id = :userId"
63
        );
64
        return $query->setParameter("userId", $userId, "ulid")->getResult()[0]["count"];
65
    }
66
}