Completed
Push — master ( bbd5c2...15f243 )
by Derek Stephen
01:52
created

UserRepository::checkCriteriaForRegistrationDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 19 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 19
        if(!$user->getID()) {
23 19
            $this->_em->persist($user);
24
        }
25 19
        $this->_em->flush($user);
26 19
        $this->_em->flush($user->getPerson());
27 19
        return $user;
28
    }
29
30
    /**
31
     * @param UserInterface $user
32
     * @param bool $deletePerson
33
     * @throws \Exception
34
     */
35 19 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...
36
    {
37 19
        if($deletePerson) {
38 18
            $this->_em->remove($user->getPerson());
39
        }
40 19
        $this->_em->remove($user);
41 19
        $this->_em->flush($user);
42 19
    }
43
44
    /**
45
     * @param UserCriteria $criteria
46
     * @return UserCriteria
47
     */
48 14
    private function checkCriteriaForId(UserCriteria $criteria)
49
    {
50 14
        if($criteria->hasId()) {
51 1
            $this->qb->where('u.id = :id');
52 1
            $this->qb->setParameter('id', $criteria->getId());
53 1
            $criteria->setLimit(1);
54
        }
55 14
        return $criteria;
56
    }
57
58
    /**
59
     * @param UserCriteria $criteria
60
     * @return UserCriteria
61
     */
62 14
    private function checkCriteriaForEmail(UserCriteria $criteria)
63
    {
64 14
        if($criteria->hasEmail()) {
65 13
            $this->qb->where('u.email = :email');
66 13
            $this->qb->setParameter('email', $criteria->getEmail());
67 13
            $criteria->setLimit(1);
68
        }
69 14
        return $criteria;
70
    }
71
72 14
    private function checkCriteriaForState(UserCriteria $criteria)
73
    {
74 14
        if($criteria->hasState()) {
75
            $this->qb->andWhere('u.state = :state');
76
            $this->qb->setParameter('state', $criteria->getState());
77
        }
78 14
    }
79
80 14
    private function checkCriteriaForRegistrationDate(UserCriteria $criteria)
81
    {
82 14
        if($criteria->hasRegistrationDate()) {
83 2
            $this->qb->andWhere('u.registrationDate = :regdate');
84 2
            $this->qb->setParameter('regdate', $criteria->getRegistrationDate());
85
        }
86 14
    }
87
88 14
    private function checkCriteriaForLastLoginDate(UserCriteria $criteria)
89
    {
90 14
        if($criteria->hasLastLoginDate()) {
91 2
            $this->qb->andWhere('u.lastLoginDate = :lastlogin');
92 2
            $this->qb->setParameter('lastlogin', $criteria->getLastLoginDate());
93
        }
94 14
    }
95
96
    /**
97
     * @param UserCriteria $criteria
98
     * @return array
99
     */
100 14
    public function findByCriteria(UserCriteria $criteria)
101
    {
102 14
        $this->qb = $this->createQueryBuilder('u');
103
104 14
        $criteria = $this->checkCriteriaForId($criteria);
105 14
        $criteria = $this->checkCriteriaForEmail($criteria);
106 14
        $this->checkCriteriaForState($criteria);
107 14
        $this->checkCriteriaForRegistrationDate($criteria);
108 14
        $this->checkCriteriaForLastLoginDate($criteria);
109
110 14
        $criteria->hasOrder() ? $this->qb->addOrderBy('u.'.$criteria->getOrder(),$criteria->getOrderDirection()) : null;
111 14
        $criteria->hasLimit() ? $this->qb->setMaxResults($criteria->getLimit()) : null;
112 14
        $criteria->hasOffset() ? $this->qb->setFirstResult($criteria->getOffset()) : null;
113
114 14
        $query = $this->qb->getQuery();
115
116 14
        return $query->getResult();
117
    }
118
}
119