Completed
Push — master ( 60435e...1588c9 )
by Derek Stephen
02:22
created

UserRepository::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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