1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Albert221\Blog\Repository\Database; |
4
|
|
|
|
5
|
|
|
use Albert221\Blog\Entity\Post; |
6
|
|
|
use Albert221\Blog\Repository\PostRepositoryInterface; |
7
|
|
|
use Doctrine\Common\Collections\Criteria; |
8
|
|
|
use Doctrine\ORM\EntityRepository; |
9
|
|
|
|
10
|
|
|
class PostRepository extends EntityRepository implements PostRepositoryInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
|
|
public function count() |
16
|
|
|
{ |
17
|
|
|
$query = $this->createQueryBuilder('p') |
18
|
|
|
->select('count(p.id)') |
19
|
|
|
->getQuery(); |
20
|
|
|
|
21
|
|
|
return $query->getSingleScalarResult(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function paginated(Criteria $criteria) |
28
|
|
|
{ |
29
|
|
|
$query = $this->createQueryBuilder('p') |
30
|
|
|
->orderBy('p.published_at', 'DESC') |
31
|
|
|
->addCriteria($criteria) |
32
|
|
|
->getQuery(); |
33
|
|
|
|
34
|
|
|
return $query->getResult(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function bySlug($slug) |
41
|
|
|
{ |
42
|
|
|
return $this->findOneBy(['slug' => $slug]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function byCategoryCount($slug) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$qb = $this->createQueryBuilder('p'); |
51
|
|
|
|
52
|
|
|
$query = $qb->select('count(p.id)') |
53
|
|
|
->join('p.category', 'c') |
54
|
|
|
->where($qb->expr()->eq('c.slug', ':category')) |
55
|
|
|
->setParameter(':category', $slug) |
56
|
|
|
->getQuery(); |
57
|
|
|
|
58
|
|
|
return $query->getSingleScalarResult(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function byCategory($slug, Criteria $criteria) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$qb = $this->createQueryBuilder('p'); |
67
|
|
|
|
68
|
|
|
$query = $qb->join('p.category', 'c') |
69
|
|
|
->where($qb->expr()->eq('c.slug', ':category')) |
70
|
|
|
->setParameter(':category', $slug) |
71
|
|
|
->orderBy('p.published_at', 'DESC') |
72
|
|
|
->addCriteria($criteria) |
73
|
|
|
->getQuery(); |
74
|
|
|
|
75
|
|
|
return $query->getResult(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function byTagCount($slug) |
79
|
|
|
{ |
80
|
|
|
$qb = $this->createQueryBuilder('p'); |
81
|
|
|
|
82
|
|
|
$query = $qb->select('count(p.id)') |
83
|
|
|
->join('p.tags', 't') |
84
|
|
|
->where($qb->expr()->eq('t.name', ':tag')) |
85
|
|
|
->setParameter(':tag', $slug) |
86
|
|
|
->getQuery(); |
87
|
|
|
|
88
|
|
|
return $query->getSingleScalarResult(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function byTag($slug, Criteria $criteria) |
95
|
|
|
{ |
96
|
|
|
$qb = $this->createQueryBuilder('p'); |
97
|
|
|
|
98
|
|
|
$query = $qb->join('p.tags', 't') |
99
|
|
|
->where($qb->expr()->eq('t.name', ':tag')) |
100
|
|
|
->setParameter(':tag', $slug) |
101
|
|
|
->orderBy('p.published_at', 'DESC') |
102
|
|
|
->addCriteria($criteria) |
103
|
|
|
->getQuery(); |
104
|
|
|
|
105
|
|
|
return $query->getResult(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function searchCount($term) |
112
|
|
|
{ |
113
|
|
|
$query = $this->createQueryBuilder('p') |
114
|
|
|
->select('COUNT(p.id)') |
115
|
|
|
->where('MATCH (p.title, p.content) AGAINST (:term) > 1') |
116
|
|
|
->setParameter(':term', $term) |
117
|
|
|
->getQuery(); |
118
|
|
|
|
119
|
|
|
return $query->getSingleScalarResult(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function search($term, Criteria $criteria) |
126
|
|
|
{ |
127
|
|
|
$query = $this->createQueryBuilder('p') |
128
|
|
|
->select( |
129
|
|
|
'p as post', |
130
|
|
|
'MATCH (p.title) AGAINST (:term) as title_relevance', |
131
|
|
|
'MATCH (p.title, p.content) AGAINST (:term) as relevance' |
132
|
|
|
) |
133
|
|
|
->where('MATCH (p.title, p.content) AGAINST (:term) > 1') |
134
|
|
|
->orderBy('title_relevance', 'DESC') |
135
|
|
|
->addOrderBy('relevance', 'DESC') |
136
|
|
|
->setParameter(':term', $term) |
137
|
|
|
->addCriteria($criteria) |
138
|
|
|
->getQuery(); |
139
|
|
|
|
140
|
|
|
$result = $query->getResult(); |
141
|
|
|
|
142
|
|
|
// because we don't want an array of post and relevances, a post only |
143
|
|
|
array_walk($result, function (&$value) { |
144
|
|
|
$value = $value['post']; |
145
|
|
|
}); |
146
|
|
|
|
147
|
|
|
return $result; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function save(Post $post) |
154
|
|
|
{ |
155
|
|
|
$this->getEntityManager()->persist($post); |
156
|
|
|
$this->getEntityManager()->flush(); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.