Completed
Push — master ( 5467e4...183ea4 )
by Boy
05:06 queued 01:02
created

RaSecondFactorRepository::createSearchQuery()   C

Complexity

Conditions 14
Paths 208

Size

Total Lines 63
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 63
rs 5.3884
cc 14
eloc 38
nc 208
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\ApiBundle\Identity\Repository;
20
21
use Doctrine\DBAL\Types\Type;
22
use Doctrine\ORM\EntityRepository;
23
use Doctrine\ORM\Query;
24
use Surfnet\Stepup\Exception\RuntimeException;
25
use Surfnet\Stepup\Identity\Value\IdentityId;
26
use Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type\SecondFactorStatusType;
27
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaSecondFactor;
28
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaSecondFactorQuery;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\SecondFactorStatus;
30
31
class RaSecondFactorRepository extends EntityRepository
32
{
33
    /**
34
     * @param string $id
35
     * @return RaSecondFactor|null
36
     */
37
    public function find($id)
38
    {
39
        /** @var RaSecondFactor|null $secondFactor */
40
        $secondFactor = parent::find($id);
41
42
        return $secondFactor;
43
    }
44
45
    /**
46
     * @param string $identityId
47
     * @return RaSecondFactor[]
48
     */
49
    public function findByIdentityId($identityId)
50
    {
51
        return parent::findBy(['identityId' => $identityId]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (findBy() instead of findByIdentityId()). Are you sure this is correct? If so, you might want to change this to $this->findBy().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
52
    }
53
54
    /**
55
     * @SuppressWarnings(PHPMD.CyclomaticComplexity) The amount of if statements do not necessarily make the method
56
     *                                               below complex or hard to maintain.
57
     * @SuppressWarnings(PHPMD.NPathComplexity)
58
     *
59
     * @param RaSecondFactorQuery $query
60
     * @return Query
61
     */
62
    public function createSearchQuery(RaSecondFactorQuery $query)
63
    {
64
        $queryBuilder = $this
65
            ->createQueryBuilder('sf')
66
            ->andWhere('sf.institution = :institution')
67
            ->setParameter('institution', $query->institution);
68
69
        if ($query->name) {
70
            $queryBuilder->andWhere('sf.name LIKE :name')->setParameter('name', sprintf('%%%s%%', $query->name));
71
        }
72
73
        if ($query->type) {
74
            $queryBuilder->andWhere('sf.type = :type')->setParameter('type', $query->type);
75
        }
76
77
        if ($query->secondFactorId) {
78
            $queryBuilder
79
                ->andWhere('sf.secondFactorId = :secondFactorId')
80
                ->setParameter('secondFactorId', $query->secondFactorId);
81
        }
82
83
        if ($query->email) {
84
            $queryBuilder->andWhere('sf.email LIKE :email')->setParameter('email', sprintf('%%%s%%', $query->email));
85
        }
86
87
        if ($query->status) {
88
            $stringStatus = $query->status;
89
            if (!SecondFactorStatus::isValidStatus($stringStatus)) {
90
                throw new RuntimeException(sprintf(
91
                    'Received invalid status "%s" in RaSecondFactorRepository::createSearchQuery',
92
                    is_object($stringStatus) ? get_class($stringStatus) : (string) $stringStatus
93
                ));
94
            }
95
96
            // we need to resolve the string value to database value using the correct doctrine type. Normally this is
97
            // done by doctrine itself, however the queries PagerFanta creates somehow manages to mangle this...
98
            // so we do it by hand
99
            $doctrineType = Type::getType(SecondFactorStatusType::NAME);
100
            $secondFactorStatus = SecondFactorStatus::$stringStatus();
101
102
            $databaseValue = $doctrineType->convertToDatabaseValue(
103
                $secondFactorStatus,
104
                $this->getEntityManager()->getConnection()->getDatabasePlatform()
105
            );
106
107
            $queryBuilder->andWhere('sf.status = :status')->setParameter('status', $databaseValue);
108
        }
109
110
        switch ($query->orderBy) {
111
            case 'name':
112
            case 'type':
113
            case 'secondFactorId':
114
            case 'email':
115
            case 'status':
116
                $queryBuilder->orderBy(
117
                    sprintf('sf.%s', $query->orderBy),
118
                    $query->orderDirection === 'desc' ? 'DESC' : 'ASC'
119
                );
120
                break;
121
        }
122
123
        return $queryBuilder->getQuery();
124
    }
125
126
    /**
127
     * @param IdentityId $identityId
128
     * @return void
129
     */
130 View Code Duplication
    public function removeByIdentityId(IdentityId $identityId)
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...
131
    {
132
        $this->getEntityManager()->createQueryBuilder()
133
            ->delete($this->_entityName, 'rasf')
134
            ->where('rasf.identityId = :identityId')
135
            ->setParameter('identityId', $identityId->getIdentityId())
136
            ->getQuery()
137
            ->execute();
138
    }
139
140
    public function save(RaSecondFactor $secondFactor)
141
    {
142
        $this->getEntityManager()->persist($secondFactor);
143
        $this->getEntityManager()->flush();
144
    }
145
146
    /**
147
     * @param RaSecondFactor[] $secondFactors
148
     */
149
    public function saveAll(array $secondFactors)
150
    {
151
        $entityManager = $this->getEntityManager();
152
153
        foreach ($secondFactors as $secondFactor) {
154
            $entityManager->persist($secondFactor);
155
        }
156
157
        $entityManager->flush();
158
    }
159
}
160