1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Del\Repository; |
4
|
|
|
|
5
|
|
|
use Del\Criteria\UserCriteria; |
6
|
|
|
use Del\Entity\UserInterface; |
7
|
|
|
use Doctrine\ORM\EntityRepository; |
8
|
|
|
use Doctrine\ORM\QueryBuilder; |
9
|
|
|
|
10
|
|
|
class UserRepository extends EntityRepository |
11
|
|
|
{ |
12
|
|
|
/** @var QueryBuilder $qb */ |
13
|
|
|
private $qb; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param UserInterface $user |
17
|
|
|
* @return UserInterface |
18
|
|
|
* @throws \Exception |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
public function save(UserInterface $user) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
if(!$user->getID()) { |
23
|
|
|
$this->_em->persist($user); |
24
|
|
|
} |
25
|
|
|
$this->_em->flush($user); |
26
|
|
|
$this->_em->flush($user->getPerson()); |
27
|
|
|
return $user; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param UserInterface $user |
32
|
|
|
* @param bool $deletePerson |
33
|
|
|
* @throws \Exception |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function delete(UserInterface $user, $deletePerson = false) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
if($deletePerson) { |
38
|
|
|
$this->_em->remove($user->getPerson()); |
39
|
|
|
} |
40
|
|
|
$this->_em->remove($user); |
41
|
|
|
$this->_em->flush($user); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param UserCriteria $criteria |
46
|
|
|
* @return UserCriteria |
47
|
|
|
*/ |
48
|
|
|
private function checkCriteriaForId(UserCriteria $criteria) |
49
|
|
|
{ |
50
|
|
|
if($criteria->hasId()) { |
51
|
|
|
$this->qb->where('u.id = :id'); |
52
|
|
|
$this->qb->setParameter('id', $criteria->getId()); |
53
|
|
|
$criteria->setLimit(1); |
54
|
|
|
} |
55
|
|
|
return $criteria; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param UserCriteria $criteria |
60
|
|
|
* @return UserCriteria |
61
|
|
|
*/ |
62
|
|
|
private function checkCriteriaForEmail(UserCriteria $criteria) |
63
|
|
|
{ |
64
|
|
|
if($criteria->hasEmail()) { |
65
|
|
|
$this->qb->where('u.email = :email'); |
66
|
|
|
$this->qb->setParameter('email', $criteria->getEmail()); |
67
|
|
|
$criteria->setLimit(1); |
68
|
|
|
} |
69
|
|
|
return $criteria; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function checkCriteriaForState(UserCriteria $criteria) |
73
|
|
|
{ |
74
|
|
|
if($criteria->hasState()) { |
75
|
|
|
$this->qb->andWhere('u.state = :state'); |
76
|
|
|
$this->qb->setParameter('state', $criteria->getState()); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function checkCriteriaForRegistrationDate(UserCriteria $criteria) |
81
|
|
|
{ |
82
|
|
|
if($criteria->hasRegistrationDate()) { |
83
|
|
|
$this->qb->andWhere('u.registrationDate = :regdate'); |
84
|
|
|
$this->qb->setParameter('regdate', $criteria->getRegistrationDate()); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function checkCriteriaForLastLoginDate(UserCriteria $criteria) |
89
|
|
|
{ |
90
|
|
|
if($criteria->hasLastLoginDate()) { |
91
|
|
|
$this->qb->andWhere('u.lastLoginDate = :lastlogin'); |
92
|
|
|
$this->qb->setParameter('lastlogin', $criteria->getLastLoginDate()); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param UserCriteria $criteria |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function findByCriteria(UserCriteria $criteria) |
101
|
|
|
{ |
102
|
|
|
$this->qb = $this->createQueryBuilder('u'); |
103
|
|
|
|
104
|
|
|
$criteria = $this->checkCriteriaForId($criteria); |
105
|
|
|
$criteria = $this->checkCriteriaForEmail($criteria); |
106
|
|
|
$this->checkCriteriaForState($criteria); |
107
|
|
|
$this->checkCriteriaForRegistrationDate($criteria); |
108
|
|
|
$this->checkCriteriaForLastLoginDate($criteria); |
109
|
|
|
|
110
|
|
|
$criteria->hasOrder() ? $this->qb->addOrderBy('u.'.$criteria->getOrder(),$criteria->getOrderDirection()) : null; |
111
|
|
|
$criteria->hasLimit() ? $this->qb->setMaxResults($criteria->getLimit()) : null; |
112
|
|
|
$criteria->hasOffset() ? $this->qb->setFirstResult($criteria->getOffset()) : null; |
113
|
|
|
|
114
|
|
|
$query = $this->qb->getQuery(); |
115
|
|
|
|
116
|
|
|
return $query->getResult(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.