|
1
|
|
|
<?php |
|
2
|
|
|
namespace AppBundle\Entity\Repository; |
|
3
|
|
|
|
|
4
|
|
|
use AppBundle\Entity\User; |
|
5
|
|
|
use AppBundle\Entity\UserRelation; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @method null|User findOneBy(array $criteria, array $orderBy = null) |
|
9
|
|
|
*/ |
|
10
|
|
|
class UserRepository extends AbstractEntityRepository |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Find all active users followed by user |
|
14
|
|
|
* |
|
15
|
|
|
* @param int $userId |
|
16
|
|
|
* |
|
17
|
|
|
* @return User[] |
|
18
|
|
|
*/ |
|
19
|
|
View Code Duplication |
public function findActiveFollowing($userId) |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
$dql = sprintf( |
|
22
|
|
|
'SELECT u FROM %s u |
|
23
|
|
|
JOIN %s ur WITH ur.nodeId = u.id |
|
24
|
|
|
WHERE ur.userId = :userId |
|
25
|
|
|
AND ur.type = :type |
|
26
|
|
|
AND u.lastAction IS NOT NULL |
|
27
|
|
|
ORDER BY u.login ASC', |
|
28
|
|
|
User::class, |
|
29
|
|
|
UserRelation::class |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
$query = $this->_em->createQuery($dql) |
|
33
|
|
|
->setParameters([ |
|
34
|
|
|
'userId' => $userId, |
|
35
|
|
|
'type' => UserRelation::TYPE_FRIEND, |
|
36
|
|
|
]); |
|
37
|
|
|
|
|
38
|
|
|
return $query->getResult(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Find all user's followers |
|
43
|
|
|
* |
|
44
|
|
|
* @param int $userId |
|
45
|
|
|
* |
|
46
|
|
|
* @return User[] |
|
47
|
|
|
*/ |
|
48
|
|
View Code Duplication |
public function findAllFollowers($userId) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$dql = sprintf( |
|
51
|
|
|
'SELECT u FROM %s u |
|
52
|
|
|
JOIN %s ur WITH ur.userId = u.id |
|
53
|
|
|
WHERE ur.nodeId = :userId |
|
54
|
|
|
AND ur.type = :type |
|
55
|
|
|
ORDER BY u.login ASC', |
|
56
|
|
|
User::class, |
|
57
|
|
|
UserRelation::class |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$query = $this->_em->createQuery($dql) |
|
61
|
|
|
->setParameters([ |
|
62
|
|
|
'userId' => $userId, |
|
63
|
|
|
'type' => UserRelation::TYPE_FRIEND, |
|
64
|
|
|
]); |
|
65
|
|
|
|
|
66
|
|
|
return $query->getResult(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Find all users followed by user |
|
71
|
|
|
* |
|
72
|
|
|
* @param int $userId |
|
73
|
|
|
* |
|
74
|
|
|
* @return User[] |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
public function findAllFollowing($userId) |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$dql = sprintf( |
|
79
|
|
|
'SELECT u FROM %s u |
|
80
|
|
|
JOIN %s ur WITH ur.nodeId = u.id |
|
81
|
|
|
WHERE ur.userId = :userId |
|
82
|
|
|
AND ur.type = :type |
|
83
|
|
|
ORDER BY u.login ASC', |
|
84
|
|
|
User::class, |
|
85
|
|
|
UserRelation::class |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
$query = $this->_em->createQuery($dql) |
|
89
|
|
|
->setParameters([ |
|
90
|
|
|
'userId' => $userId, |
|
91
|
|
|
'type' => UserRelation::TYPE_FRIEND, |
|
92
|
|
|
]); |
|
93
|
|
|
|
|
94
|
|
|
return $query->getResult(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
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.