Completed
Push — feature/fix-token-ra-candiate-... ( 2911b4 )
by
unknown
03:26
created

RaCandidateRepository   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 295
Duplicated Lines 8.81 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 7
dl 26
loc 295
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A merge() 0 6 1
A removeByIdentityId() 13 13 3
A removeIfSingleByIdentityId() 13 13 3
A removeInstitutionsNotInList() 0 17 2
A removeByRaInstitution() 0 15 3
A removeByIdentityIdAndRaInstitution() 0 11 2
A removeByNameIds() 0 11 2
B createSearchQuery() 0 47 6
A createOptionsQuery() 0 16 1
A findByNameIds() 0 8 1
A findByIdentityId() 0 8 1
A findByIdentityIdAndRaInstitution() 0 10 1
A findAllRaasByIdentityId() 0 17 1
A findByRaInstitution() 0 8 1

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
/**
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 Doctrine\ORM\Query\Expr\Join;
26
use Surfnet\Stepup\Identity\Collection\InstitutionCollection;
27
use Surfnet\Stepup\Identity\Value\IdentityId;
28
use Surfnet\Stepup\Identity\Value\Institution;
29
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
30
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContextInterface;
31
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaCandidate;
32
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\VettedSecondFactor;
33
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaCandidateQuery;
34
35
/**
36
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
37
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
38
 */
39
class RaCandidateRepository extends EntityRepository
40
{
41
    /**
42
     * @var InstitutionAuthorizationRepositoryFilter
43
     */
44
    private $authorizationRepositoryFilter;
45
46
    public function __construct(
47
        EntityManager $em,
48
        Mapping\ClassMetadata $class,
49
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
50
    ) {
51
        parent::__construct($em, $class);
52
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
53
    }
54
55
    /**
56
     * @param RaCandidate $raCandidate
57
     * @return void
58
     */
59
    public function merge(RaCandidate $raCandidate)
60
    {
61
        $raCandidate = $this->getEntityManager()->merge($raCandidate);
62
        $this->getEntityManager()->persist($raCandidate);
63
        $this->getEntityManager()->flush();
64
    }
65
66
    /**
67
     * @param IdentityId $identityId
68
     * @return void
69
     */
70 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...
71
    {
72
        $raCandidates = $this->findByIdentityId($identityId);
73
74
        if (count($raCandidates) < 1) {
75
            return;
76
        }
77
78
        foreach ($raCandidates as $candidate) {
79
            $this->getEntityManager()->remove($candidate);
80
        }
81
        $this->getEntityManager()->flush();
82
    }
83
84
85
    /**
86
     * @param IdentityId $identityId
87
     * @return void
88
     */
89 View Code Duplication
    public function removeIfSingleByIdentityId(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...
90
    {
91
        $raCandidates = $this->findByIdentityId($identityId);
92
93
        if (count($raCandidates) != 1) {
94
            return;
95
        }
96
97
        foreach ($raCandidates as $candidate) {
98
            $this->getEntityManager()->remove($candidate);
99
        }
100
        $this->getEntityManager()->flush();
101
    }
102
103
    /**
104
     * @param Institution $institution
105
     * @param InstitutionCollection $raInstitutions
106
     * @return void
107
     */
108
    public function removeInstitutionsNotInList(Institution $institution, InstitutionCollection $raInstitutions)
109
    {
110
        $raCandidates = $this->createQueryBuilder('rac')
111
            ->where('rac.raInstitution = :raInstitution')
112
            ->andWhere('rac.institution NOT IN (:institutions)')
113
            ->setParameter('raInstitution', $institution)
114
            ->setParameter('institutions', $raInstitutions->serialize())
115
            ->getQuery()
116
            ->getResult();
117
118
        $em = $this->getEntityManager();
119
        foreach ($raCandidates as $raCandidate) {
120
            $em->remove($raCandidate);
121
        }
122
123
        $em->flush();
124
    }
125
126
    /**
127
     * @param Institution $raInstitution
128
     * @return void
129
     */
130
    public function removeByRaInstitution(Institution $raInstitution)
131
    {
132
        $raCandidates = $this->findByRaInstitution($raInstitution);
133
134
        if (empty($raCandidates)) {
135
            return;
136
        }
137
138
        $em = $this->getEntityManager();
139
        foreach ($raCandidates as $raCandidate) {
140
            $em->remove($raCandidate);
141
        }
142
143
        $em->flush();
144
    }
145
146
    /**
147
     * @param IdentityId $identityId
148
     * @param Institution $raInstitution
149
     * @return void
150
     */
151
    public function removeByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
152
    {
153
        $raCandidate = $this->findByIdentityIdAndRaInstitution($identityId, $raInstitution);
154
155
        if (!$raCandidate) {
156
            return;
157
        }
158
        $em = $this->getEntityManager();
159
        $em->remove($raCandidate);
160
        $em->flush();
161
    }
162
163
    /**
164
     * @param string[] $nameIds
165
     * @return void
166
     */
167
    public function removeByNameIds($nameIds)
168
    {
169
        $raCandidates = $this->findByNameIds($nameIds);
170
171
        $em = $this->getEntityManager();
172
        foreach ($raCandidates as $raCandidate) {
173
            $em->remove($raCandidate);
174
        }
175
176
        $em->flush();
177
    }
178
179
    /**
180
     * @param RaCandidateQuery $query
181
     * @return \Doctrine\ORM\Query
182
     */
183
    public function createSearchQuery(RaCandidateQuery $query)
184
    {
185
        $queryBuilder = $this->createQueryBuilder('rac');
186
187
        // Modify query to filter on authorization
188
        $this->authorizationRepositoryFilter->filter(
189
            $queryBuilder,
190
            $query->authorizationContext,
191
            'rac.raInstitution',
192
            'iac'
193
        );
194
195
        if ($query->institution) {
196
            $queryBuilder
197
                ->andWhere('rac.institution = :institution')
198
                ->setParameter('institution', $query->institution);
199
        }
200
201
        if ($query->commonName) {
202
            $queryBuilder
203
                ->andWhere('MATCH_AGAINST(rac.commonName, :commonName) > 0')
204
                ->setParameter('commonName', $query->commonName);
205
        }
206
207
        if ($query->email) {
208
            $queryBuilder
209
                ->andWhere('MATCH_AGAINST(rac.email, :email) > 0')
210
                ->setParameter('email', $query->email);
211
        }
212
213
        if (!empty($query->secondFactorTypes)) {
214
            $queryBuilder
215
                ->innerJoin(VettedSecondFactor::class, 'vsf', Join::WITH, 'rac.identityId = vsf.identityId')
216
                ->andWhere('vsf.type IN (:secondFactorTypes)')
217
                ->setParameter('secondFactorTypes', $query->secondFactorTypes);
218
        }
219
220
        if (!empty($query->raInstitution)) {
221
            $queryBuilder
222
                ->andWhere('rac.raInstitution = :raInstitution')
223
                ->setParameter('raInstitution', $query->raInstitution);
224
        }
225
226
        $queryBuilder->groupBy('rac.identityId');
227
228
        return $queryBuilder->getQuery();
229
    }
230
231
    /**
232
     * @param RaCandidateQuery $query
233
     * @return \Doctrine\ORM\Query
234
     */
235
    public function createOptionsQuery(RaCandidateQuery $query)
236
    {
237
        $queryBuilder = $this->createQueryBuilder('rac')
238
            ->select('rac.institution')
239
            ->groupBy('rac.institution');
240
241
        // Modify query to filter on authorization
242
        $this->authorizationRepositoryFilter->filter(
243
            $queryBuilder,
244
            $query->authorizationContext,
245
            'rac.raInstitution',
246
            'iac'
247
        );
248
249
        return $queryBuilder->getQuery();
250
    }
251
252
    /**
253
     * @param string[] $sraaList
254
     * @return RaCandidate[]
255
     */
256
    public function findByNameIds(array $sraaList)
257
    {
258
        return $this->createQueryBuilder('rac')
259
            ->where('rac.nameId IN (:sraaList)')
260
            ->setParameter('sraaList', $sraaList)
261
            ->getQuery()
262
            ->getResult();
263
    }
264
265
    /**
266
     * @param string $identityId
267
     * @return RaCandidate[]
268
     * @throws \Doctrine\ORM\NonUniqueResultException
269
     */
270
    public function findByIdentityId($identityId)
271
    {
272
        return $this->createQueryBuilder('rac')
273
            ->where('rac.identityId = :identityId')
274
            ->setParameter('identityId', $identityId)
275
            ->getQuery()
276
            ->getResult();
277
    }
278
279
    /**
280
     * @param string $identityId
281
     * @param Institution $raInstitution
282
     * @return null|RaCandidate
283
     * @throws \Doctrine\ORM\NonUniqueResultException
284
     */
285
    public function findByIdentityIdAndRaInstitution($identityId, Institution $raInstitution)
286
    {
287
        return $this->createQueryBuilder('rac')
288
            ->where('rac.identityId = :identityId')
289
            ->andWhere('rac.raInstitution = :raInstitution')
290
            ->setParameter('identityId', $identityId)
291
            ->setParameter('raInstitution', $raInstitution)
292
            ->getQuery()
293
            ->getOneOrNullResult();
294
    }
295
296
297
    /**
298
     * @param string $identityId
299
     * @param InstitutionAuthorizationContextInterface $authorizationContext
300
     * @return RaCandidate[]
301
     */
302
    public function findAllRaasByIdentityId($identityId, InstitutionAuthorizationContextInterface $authorizationContext)
303
    {
304
        $queryBuilder = $this->createQueryBuilder('rac')
305
            ->where('rac.identityId = :identityId')
306
            ->setParameter('identityId', $identityId)
307
            ->orderBy('rac.raInstitution');
308
309
        // Modify query to filter on authorization
310
        $this->authorizationRepositoryFilter->filter(
311
            $queryBuilder,
312
            $authorizationContext,
313
            'rac.raInstitution',
314
            'iac'
315
        );
316
317
        return $queryBuilder->getQuery()->getResult();
318
    }
319
320
    /**
321
     * @param Institution $raInstitution
322
     * @return ArrayCollection|RaCandidate[]
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...
323
     * @throws \Doctrine\ORM\NonUniqueResultException
324
     */
325
    public function findByRaInstitution(Institution $raInstitution)
326
    {
327
        return $this->createQueryBuilder('rac')
328
            ->where('rac.raInstitution = :raInstitution')
329
            ->setParameter('raInstitution', $raInstitution)
330
            ->getQuery()
331
            ->getResult();
332
    }
333
}
334