Completed
Pull Request — develop (#261)
by
unknown
02:53
created

RaCandidateRepository::createSearchQuery()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 9.0328
c 0
b 0
f 0
cc 5
nc 16
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
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
        $queryBuilder
167
            ->andWhere('rac.raInstitution = :raInstitution')
168
            ->setParameter('raInstitution', $query->authorizationContext->getActorInstitution());
169
170
        if ($query->institution) {
171
            $queryBuilder
172
                ->andWhere('rac.institution = :institution')
173
                ->setParameter('institution', $query->institution);
174
        }
175
176
        if ($query->commonName) {
177
            $queryBuilder
178
                ->andWhere('MATCH_AGAINST(rac.commonName, :commonName) > 0')
179
                ->setParameter('commonName', $query->commonName);
180
        }
181
182
        if ($query->email) {
183
            $queryBuilder
184
                ->andWhere('MATCH_AGAINST(rac.email, :email) > 0')
185
                ->setParameter('email', $query->email);
186
        }
187
188
        if (!empty($query->secondFactorTypes)) {
189
            $queryBuilder
190
                ->innerJoin(VettedSecondFactor::class, 'vsf', Join::WITH, 'rac.identityId = vsf.identityId')
191
                ->andWhere('vsf.type IN (:secondFactorTypes)')
192
                ->setParameter('secondFactorTypes', $query->secondFactorTypes);
193
        }
194
195
        return $queryBuilder->getQuery();
196
    }
197
198
    /**
199
     * @param string[] $sraaList
200
     * @return RaCandidate[]
201
     */
202
    public function findByNameIds(array $sraaList)
203
    {
204
        return $this->createQueryBuilder('rac')
205
            ->where('rac.nameId IN (:sraaList)')
206
            ->setParameter('sraaList', $sraaList)
207
            ->getQuery()
208
            ->getResult();
209
    }
210
211
    /**
212
     * @param string $identityId
213
     * @return null|RaCandidate
214
     * @throws \Doctrine\ORM\NonUniqueResultException
215
     */
216
    public function findByIdentityId($identityId)
217
    {
218
        return $this->createQueryBuilder('rac')
219
            ->where('rac.identityId = :identityId')
220
            ->setParameter('identityId', $identityId)
221
            ->getQuery()
222
            ->getOneOrNullResult();
223
    }
224
225
    /**
226
     * @param string $identityId
227
     * @param Institution $raInstitution
228
     * @return null|RaCandidate
229
     * @throws \Doctrine\ORM\NonUniqueResultException
230
     */
231
    public function findByIdentityIdAndRaInstitution($identityId, Institution $raInstitution)
232
    {
233
        return $this->createQueryBuilder('rac')
234
            ->where('rac.identityId = :identityId')
235
            ->andWhere('rac.raInstitution = :raInstitution')
236
            ->setParameter('identityId', $identityId)
237
            ->setParameter('raInstitution', $raInstitution)
238
            ->getQuery()
239
            ->getOneOrNullResult();
240
    }
241
242
    /**
243
     * @param Institution $raInstitution
244
     * @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...
245
     * @throws \Doctrine\ORM\NonUniqueResultException
246
     */
247
    public function findByRaInstitution(Institution $raInstitution)
248
    {
249
        return $this->createQueryBuilder('rac')
250
            ->where('rac.raInstitution = :raInstitution')
251
            ->setParameter('raInstitution', $raInstitution)
252
            ->getQuery()
253
            ->getResult();
254
    }
255
}
256