1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Modules\Posts\Persistence\Doctrine\Repository; |
4
|
|
|
|
5
|
|
|
use App\Infrastructure\Pagination\Page; |
6
|
|
|
use App\Modules\Posts\Domain\Dto\PostDto; |
7
|
|
|
use App\Modules\Posts\Domain\Dto\PostForBaselineDto; |
8
|
|
|
use App\Modules\Posts\Domain\Dto\PostHeaderDto; |
9
|
|
|
use App\Modules\Posts\Domain\Repository\PostsFindingRepositoryInterface; |
10
|
|
|
use App\Modules\Posts\Persistence\Doctrine\Entity\Post; |
11
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
12
|
|
|
use Doctrine\ORM\NoResultException; |
13
|
|
|
use Symfony\Component\Uid\Ulid; |
14
|
|
|
|
15
|
|
|
class DoctrinePostsFindingRepository extends DoctrinePostsRepository implements PostsFindingRepositoryInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param Ulid $id |
20
|
|
|
* @return PostDto|null |
21
|
4 |
|
*/ |
22
|
|
|
public function findPost(Ulid $id): ?PostDto |
23
|
4 |
|
{ |
24
|
4 |
|
$dtoClass = PostDto::class; |
25
|
4 |
|
$postClass = Post::class; |
26
|
4 |
|
$query = $this->getEntityManager()->createQuery( |
27
|
|
|
"select new $dtoClass( |
28
|
4 |
|
p.id, p.title, p.body, p.tags, c.comments, p.createdById, p.createdByName, p.createdAt, p.updatedAt, p.version |
29
|
|
|
) from $postClass p join p.comments c where p.deletedAt is null and p.id = :id" |
30
|
4 |
|
); |
31
|
4 |
|
$query->setParameter("id", $id, "ulid"); |
32
|
|
|
return $query->getResult()[0]; |
33
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param int $pageNo |
38
|
2 |
|
* @return Page<PostHeaderDto> |
39
|
|
|
*/ |
40
|
2 |
|
public function findPosts(int $pageNo): Page |
41
|
2 |
|
{ |
42
|
2 |
|
$dtoClass = PostHeaderDto::class; |
43
|
2 |
|
$postClass = Post::class; |
44
|
|
|
$query = $this->getEntityManager()->createQuery( |
45
|
2 |
|
"select new $dtoClass( |
46
|
|
|
p.id, p.title, p.summary, p.tags, c.commentsCount, p.createdById, p.createdByName, p.createdAt |
47
|
|
|
) from $postClass p join p.comments c |
48
|
|
|
where p.deletedAt is null |
49
|
2 |
|
order by p.id desc" |
50
|
2 |
|
) |
51
|
|
|
->setFirstResult(($pageNo - 1) * self::PAGE_SIZE) |
52
|
2 |
|
->setMaxResults(self::PAGE_SIZE); |
53
|
2 |
|
|
54
|
2 |
|
return new Page( |
55
|
|
|
$query->getArrayResult(), |
56
|
2 |
|
$this->getCount(), |
57
|
|
|
$pageNo, |
58
|
|
|
self::PAGE_SIZE |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
2 |
|
* @return int |
64
|
|
|
*/ |
65
|
2 |
|
private function getCount(): int |
66
|
2 |
|
{ |
67
|
2 |
|
$postClass = Post::class; |
68
|
|
|
$query = $this->getEntityManager()->createQuery( |
69
|
2 |
|
"select count(p.id) as count from $postClass p where p.deletedAt is null" |
70
|
|
|
); |
71
|
|
|
return $query->getScalarResult()[0]["count"]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param \DateTime|null $from |
76
|
1 |
|
* @return array<PostForBaselineDto> |
77
|
|
|
*/ |
78
|
1 |
|
public function findExistingPostsForBaseline(?\DateTime $from): array |
79
|
1 |
|
{ |
80
|
1 |
|
$dtoClass = PostForBaselineDto::class; |
81
|
|
|
$postClass = Post::class; |
82
|
1 |
|
$dql = "select new $dtoClass( |
83
|
1 |
|
p.id, p.title, p.body, p.summary, p.tags, p.updatedAt, p.version |
84
|
|
|
) from $postClass p where p.deletedAt is null"; |
85
|
|
|
if ($from != null) { |
86
|
1 |
|
$dql = $dql . " and p.createdAt >= :from"; |
87
|
1 |
|
} |
88
|
|
|
$query = $this->getEntityManager()->createQuery( |
89
|
1 |
|
$dql |
90
|
|
|
); |
91
|
|
|
if ($from != null) { |
92
|
1 |
|
$query->setParameter("from", $from); |
93
|
|
|
} |
94
|
|
|
return $query->getResult(); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param \DateTime|null $from |
99
|
1 |
|
* @return array<Ulid> |
100
|
|
|
*/ |
101
|
1 |
|
public function findDeletedPostIdsForBaseline(?\DateTime $from): array |
102
|
1 |
|
{ |
103
|
1 |
|
$postClass = Post::class; |
104
|
|
|
$dql = "select p.id from $postClass p where p.deletedAt is not null"; |
105
|
|
|
if ($from != null) { |
106
|
1 |
|
$dql = $dql . " and p.deletedAt >= :from"; |
107
|
1 |
|
} |
108
|
|
|
$query = $this->getEntityManager()->createQuery( |
109
|
1 |
|
$dql |
110
|
|
|
); |
111
|
|
|
if ($from != null) { |
112
|
1 |
|
$query->setParameter("from", $from); |
113
|
|
|
} |
114
|
1 |
|
return $query->getResult(); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
} |