Completed
Pull Request — feature/fine-grained-authoriza... (#246)
by
unknown
440:42 queued 435:58
created

RaCandidateRepository::removeByIdentityId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
22
use Doctrine\Common\Collections\ArrayCollection;
23
use Doctrine\ORM\Mapping;
24
use Doctrine\ORM\EntityManager;
25
use Doctrine\ORM\EntityRepository;
26
use Doctrine\ORM\Query\Expr\Join;
27
use Surfnet\Stepup\Identity\Collection\InstitutionCollection;
28
use Surfnet\Stepup\Identity\Value\IdentityId;
29
use Surfnet\Stepup\Identity\Value\Institution;
30
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
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
        $this->getEntityManager()->merge($raCandidate);
62
        $this->getEntityManager()->flush();
63
    }
64
65
    /**
66
     * @param IdentityId $identityId
67
     * @return void
68
     */
69
    public function removeByIdentityId(IdentityId $identityId)
70
    {
71
        $raCandidate = $this->findByIdentityId($identityId);
72
73
        if (!$raCandidate) {
74
            return;
75
        }
76
77
        $this->getEntityManager()->remove($raCandidate);
78
        $this->getEntityManager()->flush();
79
    }
80
81
    /**
82
     * @param Institution $institution
83
     * @param InstitutionCollection $raInstitutions
84
     * @return void
85
     */
86
    public function removeInstitutionsNotInList(Institution $institution, InstitutionCollection $raInstitutions)
87
    {
88
        $raCandidates = $this->createQueryBuilder('rac')
89
            ->where('rac.raInstitution = :raInstitution')
90
            ->andWhere('rac.institution NOT IN (:institutions)')
91
            ->setParameter('raInstitution', $institution)
92
            ->setParameter('institutions', $raInstitutions)
93
            ->getQuery()
94
            ->getResult();
95
96
        $em = $this->getEntityManager();
97
        foreach ($raCandidates as $raCandidate) {
98
            $em->remove($raCandidate);
99
        }
100
101
        $em->flush();
102
    }
103
104
    /**
105
     * @param Institution $raInstitution
106
     * @return void
107
     */
108 View Code Duplication
    public function removeByRaInstitution(Institution $raInstitution)
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...
109
    {
110
        $raCandidates = $this->findByRaInstitution($raInstitution);
111
112
        if (empty($raCandidates)) {
113
            return;
114
        }
115
116
        $em = $this->getEntityManager();
117
        foreach ($raCandidates as $raCandidate) {
118
            $em->remove($raCandidate);
119
        }
120
121
        $em->flush();
122
    }
123
124
    /**
125
     * @param IdentityId $identityId
126
     * @param Institution $raInstitution
127
     * @return void
128
     */
129 View Code Duplication
    public function removeByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
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...
130
    {
131
        $raCandidates = $this->findByIdentityIdAndRaInstitution($identityId, $raInstitution);
132
133
        if (empty($raCandidates)) {
134
            return;
135
        }
136
137
        $em = $this->getEntityManager();
138
        foreach ($raCandidates as $raCandidate) {
139
            $em->remove($raCandidate);
140
        }
141
142
        $em->flush();
143
    }
144
145
    /**
146
     * @param string[] $nameIds
147
     * @return void
148
     */
149
    public function removeByNameIds($nameIds)
150
    {
151
        $raCandidates = $this->findByNameIds($nameIds);
152
153
        $em = $this->getEntityManager();
154
        foreach ($raCandidates as $raCandidate) {
155
            $em->remove($raCandidate);
156
        }
157
158
        $em->flush();
159
    }
160
161
    /**
162
     * @param RaCandidateQuery $query
163
     * @return \Doctrine\ORM\Query
164
     */
165
    public function createSearchQuery(RaCandidateQuery $query)
166
    {
167
        $queryBuilder = $this->createQueryBuilder('rac');
168
169
        // Modify query to filter on authorization
170
        $this->authorizationRepositoryFilter->filter($queryBuilder, $query->authorizationContext, 'rac.identityId', 'rac.institution', 'iac');
171
172
        if ($query->institution) {
173
            $queryBuilder
174
                ->andWhere('rac.institution = :institution')
175
                ->setParameter('institution', $query->institution);
176
        }
177
178
        if ($query->commonName) {
179
            $queryBuilder
180
                ->andWhere('MATCH_AGAINST(rac.commonName, :commonName) > 0')
181
                ->setParameter('commonName', $query->commonName);
182
        }
183
184
        if ($query->email) {
185
            $queryBuilder
186
                ->andWhere('MATCH_AGAINST(rac.email, :email) > 0')
187
                ->setParameter('email', $query->email);
188
        }
189
190
        if (!empty($query->secondFactorTypes)) {
191
            $queryBuilder
192
                ->innerJoin(VettedSecondFactor::class, 'vsf', Join::WITH, 'rac.identityId = vsf.identityId')
193
                ->andWhere('vsf.type IN (:secondFactorTypes)')
194
                ->setParameter('secondFactorTypes', $query->secondFactorTypes);
195
        }
196
197
        return $queryBuilder->getQuery();
198
    }
199
200
    /**
201
     * @param string[] $sraaList
202
     * @return RaCandidate[]
203
     */
204
    public function findByNameIds(array $sraaList)
205
    {
206
        return $this->createQueryBuilder('rac')
207
            ->where('rac.nameId IN (:sraaList)')
208
            ->setParameter('sraaList', $sraaList)
209
            ->getQuery()
210
            ->getResult();
211
    }
212
213
    /**
214
     * @param string $identityId
215
     * @return null|RaCandidate
216
     * @throws \Doctrine\ORM\NonUniqueResultException
217
     */
218
    public function findByIdentityId($identityId)
219
    {
220
        return $this->createQueryBuilder('rac')
221
            ->where('rac.identityId = :identityId')
222
            ->setParameter('identityId', $identityId)
223
            ->getQuery()
224
            ->getOneOrNullResult();
225
    }
226
227
    /**
228
     * @param string $identityId
229
     * @param Institution $raInstitution
230
     * @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...
231
     * @throws \Doctrine\ORM\NonUniqueResultException
232
     */
233
    public function findByIdentityIdAndRaInstitution($identityId, Institution $raInstitution)
234
    {
235
        return $this->createQueryBuilder('rac')
236
            ->where('rac.identityId = :identityId')
237
            ->andWhere('rac.raInstitution = :raInstitution')
238
            ->setParameter('identityId', $identityId)
239
            ->setParameter('raInstitution', $raInstitution)
240
            ->getQuery()
241
            ->getResult();
242
    }
243
244
245
    /**
246
     * @param Institution $raInstitution
247
     * @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...
248
     * @throws \Doctrine\ORM\NonUniqueResultException
249
     */
250
    public function findByRaInstitution(Institution $raInstitution)
251
    {
252
        return $this->createQueryBuilder('rac')
253
            ->where('rac.raInstitution = :raInstitution')
254
            ->setParameter('raInstitution', $raInstitution)
255
            ->getQuery()
256
            ->getResult();
257
    }
258
}
259