1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Del\Repository; |
4
|
|
|
|
5
|
|
|
use Del\Criteria\UserCriteria; |
6
|
|
|
use Del\Entity\User; |
7
|
|
|
use Doctrine\ORM\EntityRepository; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class UserRepository extends EntityRepository |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param User $user |
14
|
|
|
* @return User |
15
|
|
|
*/ |
16
|
16 |
View Code Duplication |
public function save(User $user) |
|
|
|
|
17
|
|
|
{ |
18
|
16 |
|
if(!$user->getID()) { |
19
|
16 |
|
$this->_em->persist($user); |
20
|
16 |
|
} |
21
|
16 |
|
$this->_em->flush($user); |
22
|
16 |
|
$this->_em->flush($user->getPerson()); |
23
|
16 |
|
return $user; |
24
|
|
|
} |
25
|
|
|
/** |
26
|
|
|
* @param User $user |
27
|
|
|
*/ |
28
|
16 |
View Code Duplication |
public function delete(User $user, $deletePerson = false) |
|
|
|
|
29
|
|
|
{ |
30
|
16 |
|
if($deletePerson) { |
31
|
16 |
|
$this->_em->remove($user->getPerson()); |
32
|
16 |
|
} |
33
|
16 |
|
$this->_em->remove($user); |
34
|
16 |
|
$this->_em->flush($user); |
35
|
16 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param UserCriteria $criteria |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
11 |
|
public function findByCriteria(UserCriteria $criteria) |
42
|
|
|
{ |
43
|
11 |
|
$qb = $this->createQueryBuilder('u'); |
44
|
|
|
|
45
|
11 |
|
if($criteria->hasId()) { |
46
|
1 |
|
$qb->where('u.id = :id'); |
47
|
1 |
|
$qb->setParameter('id', $criteria->getId()); |
48
|
1 |
|
$criteria->setLimit(1); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
11 |
|
if($criteria->hasEmail()) { |
52
|
10 |
|
$qb->where('u.email = :email'); |
53
|
10 |
|
$qb->setParameter('email', $criteria->getEmail()); |
54
|
10 |
|
$criteria->setLimit(1); |
55
|
10 |
|
} |
56
|
|
|
|
57
|
11 |
|
if($criteria->hasState()) { |
58
|
1 |
|
$qb->andWhere('u.state = :state'); |
59
|
1 |
|
$qb->setParameter('state', $criteria->getState()); |
60
|
1 |
|
} |
61
|
|
|
|
62
|
11 |
|
if($criteria->hasRegistrationDate()) { |
63
|
1 |
|
$qb->andWhere('u.registrationDate = :regdate'); |
64
|
1 |
|
$qb->setParameter('regdate', $criteria->getRegistrationDate()); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
11 |
|
if($criteria->hasLastLoginDate()) { |
68
|
1 |
|
$qb->andWhere('u.lastLoginDate = :lastlogin'); |
69
|
1 |
|
$qb->setParameter('lastlogin', $criteria->getLastLoginDate()); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
11 |
|
$criteria->hasOrder() ? $qb->addOrderBy('u.'.$criteria->getOrder(),$criteria->getOrderDirection()) : null; |
73
|
11 |
|
$criteria->hasLimit() ? $qb->setMaxResults($criteria->getLimit()) : null; |
74
|
11 |
|
$criteria->hasOffset() ? $qb->setFirstResult($criteria->getOffset()) : null; |
75
|
|
|
|
76
|
11 |
|
$query = $qb->getQuery(); |
77
|
|
|
|
78
|
11 |
|
return $query->getResult(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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.