|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sulu\Bundle\CommentBundle\Entity; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\ORM\NoResultException; |
|
15
|
|
|
use Sulu\Component\Persistence\Repository\ORM\EntityRepository; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Repository for querying comments. |
|
19
|
|
|
*/ |
|
20
|
|
|
class CommentRepository extends EntityRepository implements CommentRepositoryInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
|
View Code Duplication |
public function findComments($type, $entityId, $page = 1, $pageSize = null) |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$query = $this->createQueryBuilder('c') |
|
28
|
|
|
->join('c.thread', 't') |
|
29
|
|
|
->leftJoin('c.creator', 'creator') |
|
30
|
|
|
->leftJoin('c.changer', 'changer') |
|
31
|
|
|
->where('t.type = :type AND t.entityId = :entityId') |
|
32
|
|
|
->setParameter('type', $type) |
|
33
|
|
|
->setParameter('entityId', $entityId) |
|
34
|
|
|
->orderBy('c.created', 'DESC') |
|
35
|
|
|
->getQuery(); |
|
36
|
|
|
|
|
37
|
|
|
if ($pageSize) { |
|
|
|
|
|
|
38
|
|
|
$query->setMaxResults($pageSize); |
|
39
|
|
|
$query->setFirstResult(($page - 1) * $pageSize); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $query->getResult(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
1 |
View Code Duplication |
public function findPublishedComments($type, $entityId, $page, $pageSize) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
1 |
|
$query = $this->createQueryBuilder('c') |
|
51
|
1 |
|
->join('c.thread', 't') |
|
52
|
1 |
|
->leftJoin('c.creator', 'creator') |
|
53
|
1 |
|
->leftJoin('c.changer', 'changer') |
|
54
|
1 |
|
->where('c.state = :state') |
|
55
|
1 |
|
->andWhere('t.type = :type AND t.entityId = :entityId') |
|
56
|
1 |
|
->setParameter('state', CommentInterface::STATE_PUBLISHED) |
|
57
|
1 |
|
->setParameter('type', $type) |
|
58
|
1 |
|
->setParameter('entityId', $entityId) |
|
59
|
1 |
|
->orderBy('c.created', 'DESC') |
|
60
|
1 |
|
->getQuery(); |
|
61
|
|
|
|
|
62
|
1 |
|
if ($pageSize) { |
|
|
|
|
|
|
63
|
|
|
$query->setMaxResults($pageSize); |
|
64
|
|
|
$query->setFirstResult(($page - 1) * $pageSize); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
return $query->getResult(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
2 |
View Code Duplication |
public function findCommentsByIds($ids) |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
2 |
|
$query = $this->createQueryBuilder('c') |
|
76
|
2 |
|
->join('c.thread', 't') |
|
77
|
2 |
|
->leftJoin('c.creator', 'creator') |
|
78
|
2 |
|
->leftJoin('c.changer', 'changer') |
|
79
|
2 |
|
->where('c.id IN (:ids)') |
|
80
|
2 |
|
->setParameter('ids', $ids) |
|
81
|
2 |
|
->getQuery(); |
|
82
|
|
|
|
|
83
|
2 |
|
return $query->getResult(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
4 |
View Code Duplication |
public function findCommentById($id) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
4 |
|
$query = $this->createQueryBuilder('c') |
|
92
|
4 |
|
->join('c.thread', 't') |
|
93
|
4 |
|
->leftJoin('c.creator', 'creator') |
|
94
|
4 |
|
->leftJoin('c.changer', 'changer') |
|
95
|
4 |
|
->where('c.id = :id') |
|
96
|
4 |
|
->setParameter('id', $id) |
|
97
|
4 |
|
->getQuery(); |
|
98
|
|
|
|
|
99
|
|
|
try { |
|
100
|
4 |
|
return $query->getSingleResult(); |
|
101
|
|
|
} catch (NoResultException $e) { |
|
102
|
|
|
return; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
5 |
|
public function persist(CommentInterface $comment) |
|
110
|
|
|
{ |
|
111
|
5 |
|
$this->getEntityManager()->persist($comment); |
|
112
|
5 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
2 |
|
public function delete(CommentInterface $comment) |
|
118
|
|
|
{ |
|
119
|
2 |
|
$this->getEntityManager()->remove($comment); |
|
120
|
2 |
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
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.