Completed
Push — master ( e7d3f8...bbd5c2 )
by Derek Stephen
01:35
created

UserRepository::checkCriteriaForEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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)
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
        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)
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
        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