UserRepository::findByCriteria()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 11
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 17
ccs 0
cts 12
cp 0
crap 20
rs 9.9
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
    public function save(UserInterface $user)
21
    {
22
        if(!$user->getID()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user->getID() of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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
    public function delete(UserInterface $user, $deletePerson = false)
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();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->getResult() also could return the type integer which is incompatible with the documented return type array.
Loading history...
117
    }
118
}
119