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

UserRepository   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 109
Duplicated Lines 15.6 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 6
dl 17
loc 109
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 9 9 2
A delete() 8 8 2
A checkCriteriaForId() 0 9 2
A checkCriteriaForEmail() 0 9 2
A checkCriteriaForState() 0 7 2
A checkCriteriaForRegistrationDate() 0 7 2
A checkCriteriaForLastLoginDate() 0 7 2
A findByCriteria() 0 18 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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