Completed
Pull Request — develop (#265)
by Michiel
04:54 queued 02:17
created

RaListingRepository::createSearchQuery()   C

Complexity

Conditions 10
Paths 320

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 5.0521
c 0
b 0
f 0
cc 10
nc 320
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\Common\Collections\ArrayCollection;
22
use Doctrine\ORM\EntityManager;
23
use Doctrine\ORM\EntityRepository;
24
use Doctrine\ORM\Mapping;
25
use Surfnet\Stepup\Identity\Value\IdentityId;
26
use Surfnet\Stepup\Identity\Value\Institution;
27
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
28
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing;
30
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaListingQuery;
31
32
class RaListingRepository extends EntityRepository
33
{
34
    /**
35
     * @var InstitutionAuthorizationRepositoryFilter
36
     */
37
    private $authorizationRepositoryFilter;
38
39
    public function __construct(
40
        EntityManager $em,
41
        Mapping\ClassMetadata $class,
42
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
43
    ) {
44
        parent::__construct($em, $class);
45
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
46
    }
47
48
    /**
49
     * @param IdentityId $identityId The RA's identity id.
50
     * @return null|RaListing[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
51
     */
52
    public function findByIdentityId(IdentityId $identityId)
53
    {
54
        return parent::findBy(['identityId' => (string) $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...
55
    }
56
57
    /**
58
     * @param IdentityId $identityId The RA's identity id.
59
     * @param Institution $raInstitution
60
     * @return null|RaListing
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
61
     */
62
    public function findByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
63
    {
64
        return parent::findOneBy([
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (findOneBy() instead of findByIdentityIdAndRaInstitution()). Are you sure this is correct? If so, you might want to change this to $this->findOneBy().

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...
65
            'identityId' => (string) $identityId,
66
            'raInstitution' => (string) $raInstitution,
67
        ]);
68
    }
69
70
    /**
71
     * @param IdentityId $identityId The RA's identity id.
72
     * @param Institution $institution
73
     * @return RaListing[]
74
     */
75
    public function findByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution)
76
    {
77
        return parent::findBy([
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (findBy() instead of findByIdentityIdAndInstitution()). 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...
78
            'identityId' => (string) $identityId,
79
            'institution' => (string) $institution,
80
        ]);
81
    }
82
83
    public function save(RaListing $raListingEntry)
84
    {
85
        $this->getEntityManager()->persist($raListingEntry);
86
        $this->getEntityManager()->flush();
87
    }
88
89
    /**
90
     * @SuppressWarnings(PHPMD.CyclomaticComplexity) The amount of if statements do not necessarily make the method
91
     * @SuppressWarnings(PHPMD.NPathComplexity)      below complex or hard to maintain.
92
     *
93
     * @param RaListingQuery $query
94
     * @return \Doctrine\ORM\Query
95
     */
96
    public function createSearchQuery(RaListingQuery $query)
97
    {
98
        $queryBuilder = $this->createQueryBuilder('r');
99
100
        // Modify query to filter on authorization
101
        $this->authorizationRepositoryFilter->filter(
102
            $queryBuilder,
103
            $query->authorizationContext,
104
            'r.raInstitution',
105
            'iac'
106
        );
107
108
        if ($query->institution) {
109
            $queryBuilder
110
                ->andWhere('r.institution = :institution')
111
                ->setParameter('institution', $query->institution);
112
        }
113
114
        if ($query->identityId) {
115
            $queryBuilder
116
                ->andWhere('r.identityId = :identityId')
117
                ->setParameter('identityId', (string) $query->identityId);
118
        }
119
120
        if ($query->name) {
121
            $queryBuilder
122
                ->andWhere('r.commonName LIKE :name')
123
                ->setParameter('name', sprintf('%%%s%%', $query->name));
124
        }
125
126
        if ($query->email) {
127
            $queryBuilder
128
                ->andWhere('r.email LIKE :email')
129
                ->setParameter('email', sprintf('%%%s%%', $query->email));
130
        }
131
132
        if ($query->role) {
133
            $queryBuilder
134
                ->andWhere('r.role = :role')
135
                ->setParameter('role', (string) $query->role);
136
        }
137
138
        if ($query->raInstitution) {
139
            $queryBuilder
140
                ->andWhere('r.raInstitution = :raInstitution')
141
                ->setParameter('raInstitution', (string) $query->raInstitution);
142
        }
143
144
        if (!$query->orderBy) {
145
            return $queryBuilder->getQuery();
146
        }
147
148
        $orderDirection = $query->orderDirection === 'asc' ? 'ASC' : 'DESC';
149
150
        switch ($query->orderBy) {
151
            case 'commonName':
152
                $queryBuilder->orderBy('r.commonName', $orderDirection);
153
                break;
154
            default:
155
                throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy));
156
        }
157
158
        return $queryBuilder->getQuery();
159
    }
160
161
    /**
162
     * @param Institution $raInstitution
163
     * @return ArrayCollection
164
     */
165
    public function listRasFor(Institution $raInstitution)
166
    {
167
        $listings = $this->createQueryBuilder('rl')
168
            ->where('rl.raInstitution = :institution')
169
            ->setParameter('institution', $raInstitution)
170
            ->getQuery()
171
            ->getResult();
172
173
        return new ArrayCollection($listings);
174
    }
175
176
    /**
177
     * @param IdentityId $identityId
178
     * @return void
179
     */
180 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...
181
    {
182
        $this->getEntityManager()->createQueryBuilder()
183
            ->delete($this->_entityName, 'ral')
184
            ->where('ral.identityId = :identityId')
185
            ->andWhere('ral.raInstitution = :institution')
186
            ->setParameter('identityId', $identityId->getIdentityId())
187
            ->getQuery()
188
            ->execute();
189
    }
190
191
    /**
192
     * @param IdentityId $identityId
193
     * @param Institution $raInstitution
194
     * @return void
195
     */
196
    public function removeByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
197
    {
198
        $this->getEntityManager()->createQueryBuilder()
199
            ->delete($this->_entityName, 'ral')
200
            ->where('ral.identityId = :identityId')
201
            ->andWhere('ral.raInstitution = :institution')
202
            ->setParameter('identityId', $identityId->getIdentityId())
203
            ->setParameter('institution', $raInstitution)
204
            ->getQuery()
205
            ->execute();
206
    }
207
208
    /**
209
     * @param IdentityId $identityId
210
     * @param Institution $institution
211
     * @return void
212
     */
213
    public function removeByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution)
214
    {
215
        $this->getEntityManager()->createQueryBuilder()
216
            ->delete($this->_entityName, 'ral')
217
            ->where('ral.identityId = :identityId')
218
            ->andWhere('ral.institution = :institution')
219
            ->setParameter('identityId', $identityId->getIdentityId())
220
            ->setParameter('institution', $institution)
221
            ->getQuery()
222
            ->execute();
223
    }
224
}
225