Completed
Push — master ( 686b15...e1ff08 )
by Derek Stephen
08:48
created

UserRepository::findByCriteria()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.2
cc 4
eloc 12
nc 8
nop 1
crap 4
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
11
class UserRepository extends EntityRepository
12
{
13
    /** @var QueryBuilder $qb */
14
    private $qb;
15
16
    /**
17
     * @param UserInterface $user
18
     * @return UserInterface
19
     */
20 18 View Code Duplication
    public function save(UserInterface $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
21
    {
22 18
        if(!$user->getID()) {
23 18
            $this->_em->persist($user);
24
        }
25 18
        $this->_em->flush($user);
26 18
        $this->_em->flush($user->getPerson());
27 18
        return $user;
28
    }
29
    /**
30
     * @param UserInterface $user
31
     */
32 18 View Code Duplication
    public function delete(UserInterface $user, $deletePerson = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
33
    {
34 18
        if($deletePerson) {
35 17
            $this->_em->remove($user->getPerson());
36
        }
37 18
        $this->_em->remove($user);
38 18
        $this->_em->flush($user);
39 18
    }
40
41
    /**
42
     * @param UserCriteria $criteria
43
     * @return UserCriteria
44
     */
45 14
    private function checkCriteriaForId(UserCriteria $criteria)
46
    {
47 14
        if($criteria->hasId()) {
48 1
            $this->qb->where('u.id = :id');
49 1
            $this->qb->setParameter('id', $criteria->getId());
50 1
            $criteria->setLimit(1);
51
        }
52 14
        return $criteria;
53
    }
54
55
    /**
56
     * @param UserCriteria $criteria
57
     * @return UserCriteria
58
     */
59 14
    private function checkCriteriaForEmail(UserCriteria $criteria)
60
    {
61 14
        if($criteria->hasEmail()) {
62 13
            $this->qb->where('u.email = :email');
63 13
            $this->qb->setParameter('email', $criteria->getEmail());
64 13
            $criteria->setLimit(1);
65
        }
66 14
        return $criteria;
67
    }
68
69 14
    private function checkCriteriaForState(UserCriteria $criteria)
70
    {
71 14
        if($criteria->hasState()) {
72 2
            $this->qb->andWhere('u.state = :state');
73 2
            $this->qb->setParameter('state', $criteria->getState());
74
        }
75 14
    }
76
77 14
    private function checkCriteriaForRegistrationDate(UserCriteria $criteria)
78
    {
79 14
        if($criteria->hasRegistrationDate()) {
80 2
            $this->qb->andWhere('u.registrationDate = :regdate');
81 2
            $this->qb->setParameter('regdate', $criteria->getRegistrationDate());
82
        }
83 14
    }
84
85 14
    private function checkCriteriaForLastLoginDate(UserCriteria $criteria)
86
    {
87 14
        if($criteria->hasLastLoginDate()) {
88 2
            $this->qb->andWhere('u.lastLoginDate = :lastlogin');
89 2
            $this->qb->setParameter('lastlogin', $criteria->getLastLoginDate());
90
        }
91 14
    }
92
93
    /**
94
     * @param UserCriteria $criteria
95
     * @return array
96
     */
97 14
    public function findByCriteria(UserCriteria $criteria)
98
    {
99 14
        $this->qb = $this->createQueryBuilder('u');
100
101 14
        $criteria = $this->checkCriteriaForId($criteria);
102 14
        $criteria = $this->checkCriteriaForEmail($criteria);
103 14
        $this->checkCriteriaForState($criteria);
104 14
        $this->checkCriteriaForRegistrationDate($criteria);
105 14
        $this->checkCriteriaForLastLoginDate($criteria);
106
107 14
        $criteria->hasOrder() ? $this->qb->addOrderBy('u.'.$criteria->getOrder(),$criteria->getOrderDirection()) : null;
108 14
        $criteria->hasLimit() ? $this->qb->setMaxResults($criteria->getLimit()) : null;
109 14
        $criteria->hasOffset() ? $this->qb->setFirstResult($criteria->getOffset()) : null;
110
111 14
        $query = $this->qb->getQuery();
112
113 14
        return $query->getResult();
114
    }
115
}
116