Completed
Branch master (3a823b)
by Derek Stephen
04:04 queued 02:12
created

UserRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 23.94 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 6
dl 17
loc 71
ccs 43
cts 43
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 9 9 2
A delete() 8 8 2
F findByCriteria() 0 39 9

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\User;
7
use Doctrine\ORM\EntityRepository;
8
9
10
class UserRepository extends EntityRepository
11
{
12
    /**
13
     * @param User $user
14
     * @return User
15
     */
16 16 View Code Duplication
    public function save(User $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...
17
    {
18 16
        if(!$user->getID()) {
19 16
            $this->_em->persist($user);
20 16
        }
21 16
        $this->_em->flush($user);
22 16
        $this->_em->flush($user->getPerson());
23 16
        return $user;
24
    }
25
    /**
26
     * @param User $user
27
     */
28 16 View Code Duplication
    public function delete(User $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...
29
    {
30 16
        if($deletePerson) {
31 16
            $this->_em->remove($user->getPerson());
32 16
        }
33 16
        $this->_em->remove($user);
34 16
        $this->_em->flush($user);
35 16
    }
36
37
    /**
38
     * @param UserCriteria $criteria
39
     * @return array
40
     */
41 11
    public function findByCriteria(UserCriteria $criteria)
42
    {
43 11
        $qb = $this->createQueryBuilder('u');
44
45 11
        if($criteria->hasId()) {
46 1
            $qb->where('u.id = :id');
47 1
            $qb->setParameter('id', $criteria->getId());
48 1
            $criteria->setLimit(1);
49 1
        }
50
51 11
        if($criteria->hasEmail()) {
52 10
            $qb->where('u.email = :email');
53 10
            $qb->setParameter('email', $criteria->getEmail());
54 10
            $criteria->setLimit(1);
55 10
        }
56
57 11
        if($criteria->hasState()) {
58 1
            $qb->andWhere('u.state = :state');
59 1
            $qb->setParameter('state', $criteria->getState());
60 1
        }
61
62 11
        if($criteria->hasRegistrationDate()) {
63 1
            $qb->andWhere('u.registrationDate = :regdate');
64 1
            $qb->setParameter('regdate', $criteria->getRegistrationDate());
65 1
        }
66
67 11
        if($criteria->hasLastLoginDate()) {
68 1
            $qb->andWhere('u.lastLoginDate = :lastlogin');
69 1
            $qb->setParameter('lastlogin', $criteria->getLastLoginDate());
70 1
        }
71
72 11
        $criteria->hasOrder() ? $qb->addOrderBy('u.'.$criteria->getOrder(),$criteria->getOrderDirection()) : null;
73 11
        $criteria->hasLimit() ? $qb->setMaxResults($criteria->getLimit()) : null;
74 11
        $criteria->hasOffset() ? $qb->setFirstResult($criteria->getOffset()) : null;
75
76 11
        $query = $qb->getQuery();
77
78 11
        return $query->getResult();
79
    }
80
}
81