Completed
Pull Request — develop (#267)
by
unknown
06:14 queued 03:05
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\Authorization\Value\InstitutionAuthorizationContextInterface;
29
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException;
30
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing;
31
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaListingQuery;
32
33
/**
34
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
35
 */
36
class RaListingRepository extends EntityRepository
37
{
38
    /**
39
     * @var InstitutionAuthorizationRepositoryFilter
40
     */
41
    private $authorizationRepositoryFilter;
42
43
    public function __construct(
44
        EntityManager $em,
45
        Mapping\ClassMetadata $class,
46
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
47
    ) {
48
        parent::__construct($em, $class);
49
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
50
    }
51
52
    /**
53
     * @param IdentityId $identityId The RA's identity id.
54
     * @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...
55
     */
56
    public function findByIdentityId(IdentityId $identityId)
57
    {
58
        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...
59
    }
60
61
    /**
62
     * @param IdentityId $identityId The RA's identity id.
63
     * @param Institution $raInstitution
64
     * @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...
65
     */
66
    public function findByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
67
    {
68
        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...
69
            'identityId' => (string) $identityId,
70
            'raInstitution' => (string) $raInstitution,
71
        ]);
72
    }
73
74
75
    /**
76
     * @param IdentityId $identityId The RA's identity id.
77
     * @param Institution $raInstitution
78
     * @param InstitutionAuthorizationContextInterface $authorizationContext
79
     * @return null|RaListing
80
     */
81
    public function findByIdentityIdAndRaInstitutionWithContext(IdentityId $identityId, Institution $raInstitution, InstitutionAuthorizationContextInterface $authorizationContext)
82
    {
83
        $queryBuilder = $this->createQueryBuilder('r')
84
            ->where('r.identityId = :identityId')
85
            ->andWhere('r.raInstitution = :raInstitution')
86
            ->setParameter('identityId', $identityId)
87
            ->setParameter('raInstitution', (string)$raInstitution)
88
            ->orderBy('r.raInstitution');
89
90
        // Modify query to filter on authorization
91
        $this->authorizationRepositoryFilter->filter(
92
            $queryBuilder,
93
            $authorizationContext,
94
            'r.raInstitution',
95
            'iac'
96
        );
97
98
        return $queryBuilder->getQuery()->getOneOrNullResult();
99
    }
100
101
    /**
102
     * @param IdentityId $identityId The RA's identity id.
103
     * @param Institution $institution
104
     * @return RaListing[]
105
     */
106
    public function findByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution)
107
    {
108
        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...
109
            'identityId' => (string) $identityId,
110
            'institution' => (string) $institution,
111
        ]);
112
    }
113
114
    public function save(RaListing $raListingEntry)
115
    {
116
        $this->getEntityManager()->persist($raListingEntry);
117
        $this->getEntityManager()->flush();
118
    }
119
120
    /**
121
     * @SuppressWarnings(PHPMD.CyclomaticComplexity) The amount of if statements do not necessarily make the method
122
     * @SuppressWarnings(PHPMD.NPathComplexity)      below complex or hard to maintain.
123
     *
124
     * @param RaListingQuery $query
125
     * @return \Doctrine\ORM\Query
126
     */
127
    public function createSearchQuery(RaListingQuery $query)
128
    {
129
        $queryBuilder = $this->createQueryBuilder('r');
130
131
        // Modify query to filter on authorization
132
        $this->authorizationRepositoryFilter->filter(
133
            $queryBuilder,
134
            $query->authorizationContext,
135
            'r.raInstitution',
136
            'iac'
137
        );
138
139
        if ($query->institution) {
140
            $queryBuilder
141
                ->andWhere('r.institution = :institution')
142
                ->setParameter('institution', $query->institution);
143
        }
144
145
        if ($query->identityId) {
146
            $queryBuilder
147
                ->andWhere('r.identityId = :identityId')
148
                ->setParameter('identityId', (string) $query->identityId);
149
        }
150
151
        if ($query->name) {
152
            $queryBuilder
153
                ->andWhere('r.commonName LIKE :name')
154
                ->setParameter('name', sprintf('%%%s%%', $query->name));
155
        }
156
157
        if ($query->email) {
158
            $queryBuilder
159
                ->andWhere('r.email LIKE :email')
160
                ->setParameter('email', sprintf('%%%s%%', $query->email));
161
        }
162
163
        if ($query->role) {
164
            $queryBuilder
165
                ->andWhere('r.role = :role')
166
                ->setParameter('role', (string) $query->role);
167
        }
168
169
        if ($query->raInstitution) {
170
            $queryBuilder
171
                ->andWhere('r.raInstitution = :raInstitution')
172
                ->setParameter('raInstitution', (string) $query->raInstitution);
173
        }
174
175
        if (!$query->orderBy) {
176
            return $queryBuilder->getQuery();
177
        }
178
179
        $orderDirection = $query->orderDirection === 'asc' ? 'ASC' : 'DESC';
180
181
        switch ($query->orderBy) {
182
            case 'commonName':
183
                $queryBuilder->orderBy('r.commonName', $orderDirection);
184
                break;
185
            default:
186
                throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy));
187
        }
188
189
        return $queryBuilder->getQuery();
190
    }
191
192
    /**
193
     * @param Institution $raInstitution
194
     * @return ArrayCollection
195
     */
196
    public function listRasFor(Institution $raInstitution)
197
    {
198
        $listings = $this->createQueryBuilder('rl')
199
            ->where('rl.raInstitution = :institution')
200
            ->setParameter('institution', $raInstitution)
201
            ->getQuery()
202
            ->getResult();
203
204
        return new ArrayCollection($listings);
205
    }
206
207
    /**
208
     * @param IdentityId $identityId
209
     * @return void
210
     */
211 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...
212
    {
213
        $this->getEntityManager()->createQueryBuilder()
214
            ->delete($this->_entityName, 'ral')
215
            ->where('ral.identityId = :identityId')
216
            ->andWhere('ral.raInstitution = :institution')
217
            ->setParameter('identityId', $identityId->getIdentityId())
218
            ->getQuery()
219
            ->execute();
220
    }
221
222
    /**
223
     * @param IdentityId $identityId
224
     * @param Institution $raInstitution
225
     * @return void
226
     */
227
    public function removeByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
228
    {
229
        $this->getEntityManager()->createQueryBuilder()
230
            ->delete($this->_entityName, 'ral')
231
            ->where('ral.identityId = :identityId')
232
            ->andWhere('ral.raInstitution = :institution')
233
            ->setParameter('identityId', $identityId->getIdentityId())
234
            ->setParameter('institution', $raInstitution)
235
            ->getQuery()
236
            ->execute();
237
    }
238
239
    /**
240
     * @param IdentityId $identityId
241
     * @param Institution $institution
242
     * @return void
243
     */
244
    public function removeByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution)
245
    {
246
        $this->getEntityManager()->createQueryBuilder()
247
            ->delete($this->_entityName, 'ral')
248
            ->where('ral.identityId = :identityId')
249
            ->andWhere('ral.institution = :institution')
250
            ->setParameter('identityId', $identityId->getIdentityId())
251
            ->setParameter('institution', $institution)
252
            ->getQuery()
253
            ->execute();
254
    }
255
}
256