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