InstitutionAuthorizationRepository   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 141
rs 10
c 0
b 0
f 0
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A findSelectRaasForInstitution() 0 9 1
A __construct() 0 3 1
A clearOldAuthorizations() 0 14 1
A saveInstitutionOption() 0 15 2
A save() 0 8 1
A setDefaultInstitutionOption() 0 13 1
A findAuthorizationOptionsForInstitution() 0 7 1
A findAuthorizationOptionsForInstitutionByRole() 0 9 1
A addNewAuthorizations() 0 8 3
A clearInstitutionOption() 0 12 1
1
<?php
2
3
/**
4
 * Copyright 2018 SURFnet B.V.
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\Configuration\Repository;
20
21
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
22
use Doctrine\ORM\EntityManagerInterface;
23
use Doctrine\ORM\OptimisticLockException;
24
use Doctrine\Persistence\ManagerRegistry;
25
use Surfnet\Stepup\Configuration\Value\Institution;
26
use Surfnet\Stepup\Configuration\Value\InstitutionAuthorizationOption;
27
use Surfnet\Stepup\Configuration\Value\InstitutionRole;
28
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionAuthorization;
29
30
/**
31
 * @extends ServiceEntityRepository<InstitutionAuthorization>
32
 */
33
class InstitutionAuthorizationRepository extends ServiceEntityRepository
34
{
35
    public function __construct(ManagerRegistry $registry)
36
    {
37
        parent::__construct($registry, InstitutionAuthorization::class);
38
    }
39
40
    /**
41
     * @return InstitutionAuthorization[]
42
     */
43
    public function findAuthorizationOptionsForInstitutionByRole(Institution $institution, InstitutionRole $role): array
44
    {
45
        return $this->createQueryBuilder('ia')
46
            ->where('ia.institution = :institution')
47
            ->andWhere('ia.role = :role')
48
            ->setParameter('institution', $institution->getInstitution())
49
            ->setParameter('institutionRole', $role)
50
            ->getQuery()
51
            ->getResult();
52
    }
53
54
    /**
55
     * @return InstitutionAuthorization[]
56
     */
57
    public function findAuthorizationOptionsForInstitution(Institution $institution): array
58
    {
59
        return $this->createQueryBuilder('ia')
60
            ->where('ia.institution = :institution')
61
            ->setParameter('institution', $institution->getInstitution())
62
            ->getQuery()
63
            ->getResult();
64
    }
65
66
    /**
67
     * @return InstitutionAuthorization[]
68
     */
69
    public function findSelectRaasForInstitution(Institution $institution): array
70
    {
71
        return $this->createQueryBuilder('ia')
72
            ->where('ia.institutionRelation = :institution')
73
            ->andWhere('ia.institutionRole = :role')
74
            ->setParameter('institution', $institution->getInstitution())
75
            ->setParameter('role', InstitutionRole::selectRaa()->getType())
76
            ->getQuery()
77
            ->getResult();
78
    }
79
80
    /**
81
     * @throws OptimisticLockException
82
     */
83
    public function saveInstitutionOption(
84
        Institution $institution,
85
        InstitutionAuthorizationOption $institutionOption,
86
    ): void {
87
        $institutionAuthorizations = [];
88
89
        foreach ($institutionOption->getInstitutions($institution) as $relatedInstitution) {
90
            $institutionAuthorizations[] = InstitutionAuthorization::create(
91
                $institution,
92
                $relatedInstitution,
93
                $institutionOption->getInstitutionRole(),
94
            );
95
        }
96
97
        $this->save($institution, $institutionOption->getInstitutionRole(), $institutionAuthorizations);
98
    }
99
100
    public function clearInstitutionOption(Institution $institution): void
101
    {
102
        $entityManager = $this->getEntityManager();
103
104
        $entityManager->createQuery(
105
            'DELETE ' . InstitutionAuthorization::class . ' ia
106
            WHERE ia.institution = :institution',
107
        )
108
            ->setParameter('institution', $institution->getInstitution())
109
            ->execute();
110
111
        $entityManager->flush();
112
    }
113
114
115
    /**
116
     * @throws OptimisticLockException
117
     */
118
    public function setDefaultInstitutionOption(Institution $institution): void
119
    {
120
        $this->saveInstitutionOption(
121
            $institution,
122
            InstitutionAuthorizationOption::getDefault(InstitutionRole::useRa()),
123
        );
124
        $this->saveInstitutionOption(
125
            $institution,
126
            InstitutionAuthorizationOption::getDefault(InstitutionRole::useRaa()),
127
        );
128
        $this->saveInstitutionOption(
129
            $institution,
130
            InstitutionAuthorizationOption::getDefault(InstitutionRole::selectRaa()),
131
        );
132
    }
133
134
    /**
135
     * @param InstitutionAuthorization[] $institutionAuthorizations
136
     */
137
    private function save(Institution $institution, InstitutionRole $role, array $institutionAuthorizations): void
138
    {
139
        $entityManager = $this->getEntityManager();
140
141
        $this->clearOldAuthorizations($entityManager, $institution, $role);
142
        $this->addNewAuthorizations($entityManager, $role, $institutionAuthorizations);
143
144
        $entityManager->flush();
145
    }
146
147
    private function clearOldAuthorizations(
148
        EntityManagerInterface $entityManager,
149
        Institution $institution,
150
        InstitutionRole $role,
151
    ): void {
152
        $entityManager->createQuery(
153
            'DELETE ' . InstitutionAuthorization::class . ' ia
154
            WHERE ia.institutionRole = :role AND ia.institution = :institution',
155
        )
156
            ->setParameter('role', $role)
157
            ->setParameter('institution', $institution->getInstitution())
158
            ->execute();
159
160
        $this->getEntityManager()->clear();
161
    }
162
163
    /**
164
     * @param InstitutionAuthorization[] $institutionAuthorizations
165
     */
166
    private function addNewAuthorizations(
167
        EntityManagerInterface $entityManager,
168
        InstitutionRole $role,
169
        array $institutionAuthorizations,
170
    ): void {
171
        foreach ($institutionAuthorizations as $institutionAuthorization) {
172
            if ($institutionAuthorization->institutionRole === $role) {
173
                $entityManager->persist($institutionAuthorization);
174
            }
175
        }
176
    }
177
}
178