Completed
Pull Request — develop (#261)
by Michiel
04:59 queued 02:35
created

findAllRaasByIdentityIdAndRaInstitution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Mapping;
23
use Doctrine\ORM\EntityManager;
24
use Doctrine\ORM\EntityRepository;
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\Identity\Entity\RaCandidate;
31
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\VettedSecondFactor;
32
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaCandidateQuery;
33
34
/**
35
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
36
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
37
 */
38
class RaCandidateRepository extends EntityRepository
39
{
40
    /**
41
     * @var InstitutionAuthorizationRepositoryFilter
42
     */
43
    private $authorizationRepositoryFilter;
44
45
    public function __construct(
46
        EntityManager $em,
47
        Mapping\ClassMetadata $class,
48
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
49
    ) {
50
        parent::__construct($em, $class);
51
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
52
    }
53
54
    /**
55
     * @param RaCandidate $raCandidate
56
     * @return void
57
     */
58
    public function merge(RaCandidate $raCandidate)
59
    {
60
        $raCandidate = $this->getEntityManager()->merge($raCandidate);
61
        $this->getEntityManager()->persist($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->serialize())
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
    public function removeByRaInstitution(Institution $raInstitution)
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
    public function removeByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
130
    {
131
        $raCandidate = $this->findByIdentityIdAndRaInstitution($identityId, $raInstitution);
132
133
        if (!$raCandidate) {
134
            return;
135
        }
136
        $em = $this->getEntityManager();
137
        $em->remove($raCandidate);
138
        $em->flush();
139
    }
140
141
    /**
142
     * @param string[] $nameIds
143
     * @return void
144
     */
145
    public function removeByNameIds($nameIds)
146
    {
147
        $raCandidates = $this->findByNameIds($nameIds);
148
149
        $em = $this->getEntityManager();
150
        foreach ($raCandidates as $raCandidate) {
151
            $em->remove($raCandidate);
152
        }
153
154
        $em->flush();
155
    }
156
157
    /**
158
     * @param RaCandidateQuery $query
159
     * @return \Doctrine\ORM\Query
160
     */
161
    public function createSearchQuery(RaCandidateQuery $query)
162
    {
163
        $queryBuilder = $this->createQueryBuilder('rac');
164
165
        // Modify query to filter on authorization
166
        $this->authorizationRepositoryFilter->filterCandidate(
167
            $queryBuilder,
168
            $query->authorizationContext->getActorInstitution(),
169
            'rac.identityId',
170
            'rac.raInstitution',
171
            'iac'
172
        );
173
174
        if ($query->institution) {
175
            $queryBuilder
176
                ->andWhere('rac.institution = :institution')
177
                ->setParameter('institution', $query->institution);
178
        }
179
180
        if ($query->commonName) {
181
            $queryBuilder
182
                ->andWhere('MATCH_AGAINST(rac.commonName, :commonName) > 0')
183
                ->setParameter('commonName', $query->commonName);
184
        }
185
186
        if ($query->email) {
187
            $queryBuilder
188
                ->andWhere('MATCH_AGAINST(rac.email, :email) > 0')
189
                ->setParameter('email', $query->email);
190
        }
191
192
        if (!empty($query->secondFactorTypes)) {
193
            $queryBuilder
194
                ->innerJoin(VettedSecondFactor::class, 'vsf', Join::WITH, 'rac.identityId = vsf.identityId')
195
                ->andWhere('vsf.type IN (:secondFactorTypes)')
196
                ->setParameter('secondFactorTypes', $query->secondFactorTypes);
197
        }
198
199
        return $queryBuilder->getQuery();
200
    }
201
202
    /**
203
     * @param string[] $sraaList
204
     * @return RaCandidate[]
205
     */
206
    public function findByNameIds(array $sraaList)
207
    {
208
        return $this->createQueryBuilder('rac')
209
            ->where('rac.nameId IN (:sraaList)')
210
            ->setParameter('sraaList', $sraaList)
211
            ->getQuery()
212
            ->getResult();
213
    }
214
215
    /**
216
     * @param string $identityId
217
     * @return null|RaCandidate
218
     * @throws \Doctrine\ORM\NonUniqueResultException
219
     */
220
    public function findByIdentityId($identityId)
221
    {
222
        return $this->createQueryBuilder('rac')
223
            ->where('rac.identityId = :identityId')
224
            ->setParameter('identityId', $identityId)
225
            ->getQuery()
226
            ->getOneOrNullResult();
227
    }
228
229
    /**
230
     * @param string $identityId
231
     * @param Institution $raInstitution
232
     * @return null|RaCandidate
233
     * @throws \Doctrine\ORM\NonUniqueResultException
234
     */
235
    public function findByIdentityIdAndRaInstitution($identityId, Institution $raInstitution)
236
    {
237
        return $this->createQueryBuilder('rac')
238
            ->where('rac.identityId = :identityId')
239
            ->andWhere('rac.raInstitution = :raInstitution')
240
            ->setParameter('identityId', $identityId)
241
            ->setParameter('raInstitution', $raInstitution)
242
            ->getQuery()
243
            ->getOneOrNullResult();
244
    }
245
246
247
    /**
248
     * @param string $identityId
249
     * @param Institution $raInstitution
250
     * @return RaCandidate[]
251
     */
252
    public function findAllRaasByIdentityIdAndRaInstitution($identityId, Institution $raInstitution)
253
    {
254
        $queryBuilder = $this->createQueryBuilder('rac')
255
            ->where('rac.identityId = :identityId')
256
            ->setParameter('identityId', $identityId)
257
            ->orderBy('rac.raInstitution');
258
259
            // Modify query to filter on authorization
260
        $this->authorizationRepositoryFilter->filterCandidateReverse(
261
            $queryBuilder,
262
            $raInstitution,
263
            'rac.raInstitution',
264
            'rac.raInstitution',
265
            'iac'
266
        );
267
268
        return $queryBuilder->getQuery()->getResult();
269
    }
270
271
    /**
272
     * @param Institution $raInstitution
273
     * @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...
274
     * @throws \Doctrine\ORM\NonUniqueResultException
275
     */
276
    public function findByRaInstitution(Institution $raInstitution)
277
    {
278
        return $this->createQueryBuilder('rac')
279
            ->where('rac.raInstitution = :raInstitution')
280
            ->setParameter('raInstitution', $raInstitution)
281
            ->getQuery()
282
            ->getResult();
283
    }
284
}
285