Completed
Pull Request — feature/fine-grained-authoriza... (#246)
by
unknown
40:56 queued 21:52
created

findSelectRaasForInstitution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
rs 9.9332
cc 1
nc 1
nop 2
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\ORM\EntityManager;
22
use Doctrine\ORM\EntityRepository;
23
use Surfnet\Stepup\Configuration\Value\Institution;
24
use Surfnet\Stepup\Configuration\Value\InstitutionAuthorizationOption;
25
use Surfnet\Stepup\Configuration\Value\InstitutionRole;
26
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionAuthorization;
27
28
class InstitutionAuthorizationRepository extends EntityRepository
29
{
30
31
    /**
32
     * @param Institution $institution
33
     * @param InstitutionRole $role
34
     * @return InstitutionAuthorization[]
35
     */
36 View Code Duplication
    public function findAuthorizationOptionsForInstitutionByRole(Institution $institution, InstitutionRole $role)
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...
37
    {
38
        return $this->createQueryBuilder('ia')
39
            ->where('ia.institution = :institution')
40
            ->andWhere('ia.role = :role')
41
            ->setParameter('institution', $institution->getInstitution())
42
            ->setParameter('institutionRole', $role)
43
            ->getQuery()
44
            ->getResult();
45
    }
46
47
    /**
48
     * @param Institution $institution
49
     * @return InstitutionAuthorization[]
50
     */
51
    public function findAuthorizationOptionsForInstitution(Institution $institution)
52
    {
53
        return $this->createQueryBuilder('ia')
54
            ->where('ia.institution = :institution')
55
            ->setParameter('institution', $institution->getInstitution())
56
            ->getQuery()
57
            ->getResult();
58
    }
59
60
    /**
61
     * @param Institution $institution
62
     * @param InstitutionRole $role
63
     * @return InstitutionAuthorization[]
64
     */
65 View Code Duplication
    public function findSelectRaasForInstitution(Institution $institution, InstitutionRole $role)
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...
66
    {
67
        return $this->createQueryBuilder('ia')
68
            ->where('ia.institutionRelation = :institution')
69
            ->andWhere('ia.institutionRole = :role')
70
            ->setParameter('institution', $institution->getInstitution())
71
            ->setParameter('role', $role->getType())
72
            ->getQuery()
73
            ->getResult();
74
    }
75
76
    /**
77
     * @param Institution $institution
78
     * @param InstitutionAuthorizationOption $institutionOption
79
     * @throws \Doctrine\ORM\OptimisticLockException
80
     */
81
    public function saveInstitutionOption(Institution $institution, InstitutionAuthorizationOption $institutionOption)
82
    {
83
        $institutionAuthorizations = [];
84
85
        foreach ($institutionOption->getInstitutions($institution) as $relatedInstitution) {
86
            $institutionAuthorizations[] = InstitutionAuthorization::create(
87
                $institution,
88
                $relatedInstitution,
89
                $institutionOption->getInstitutionRole()
90
            );
91
        }
92
93
        $this->save($institution, $institutionOption->getInstitutionRole(), $institutionAuthorizations);
94
    }
95
96
    /**
97
     * @param Institution $institution
98
     * @param InstitutionAuthorizationOption $institutionOption
0 ignored issues
show
Documentation introduced by
There is no parameter named $institutionOption. Did you maybe mean $institution?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
99
     * @throws \Doctrine\ORM\OptimisticLockException
100
     */
101
    public function clearInstitutionOption(Institution $institution)
102
    {
103
        $entityManager = $this->getEntityManager();
104
105
        $entityManager->createQuery(
106
            'DELETE '.InstitutionAuthorization::class.' ia
107
            WHERE ia.institution = :institution'
108
        )
109
            ->setParameter('institution', $institution->getInstitution())
110
            ->execute();
111
112
        $entityManager->flush();
113
    }
114
115
116
    /**
117
     * @param Institution $institution
118
     * @throws \Doctrine\ORM\OptimisticLockException
119
     */
120
    public function setDefaultInstitutionOption(Institution $institution)
121
    {
122
        $this->saveInstitutionOption(
123
            $institution,
124
            InstitutionAuthorizationOption::getDefault(InstitutionRole::useRa())
125
        );
126
        $this->saveInstitutionOption(
127
            $institution,
128
            InstitutionAuthorizationOption::getDefault(InstitutionRole::useRaa())
129
        );
130
        $this->saveInstitutionOption(
131
            $institution,
132
            InstitutionAuthorizationOption::getDefault(InstitutionRole::selectRaa())
133
        );
134
    }
135
136
    /**
137
     * @param Institution $institution
138
     * @param InstitutionRole $role
139
     * @param InstitutionAuthorization[] $institutionAuthorizations
140
     * @throws \Doctrine\ORM\OptimisticLockException
141
     */
142
    private function save(Institution $institution, InstitutionRole $role, array $institutionAuthorizations)
143
    {
144
        $entityManager = $this->getEntityManager();
145
146
        $this->clearOldAuthorizations($entityManager, $institution, $role);
147
        $this->addNewAuthorizations($entityManager, $role, $institutionAuthorizations);
148
149
        $entityManager->flush();
150
    }
151
152
    /**
153
     * @param EntityManager $entityManager
154
     * @param Institution $institution
155
     * @param InstitutionRole $role
156
     */
157
    private function clearOldAuthorizations(EntityManager $entityManager, Institution $institution, InstitutionRole $role)
158
    {
159
        $entityManager->createQuery(
160
            'DELETE '.InstitutionAuthorization::class.' ia
161
            WHERE ia.institutionRole = :role AND ia.institution = :institution'
162
        )
163
            ->setParameter('role', $role)
164
            ->setParameter('institution', $institution->getInstitution())
165
            ->execute();
166
    }
167
168
    /**
169
     * @param EntityManager $entityManager
170
     * @param InstitutionRole $role
171
     * @param InstitutionAuthorization[] $institutionAuthorizations
172
     */
173
    private function addNewAuthorizations(EntityManager $entityManager, InstitutionRole $role, array $institutionAuthorizations)
174
    {
175
        foreach ($institutionAuthorizations as $institutionAuthorization) {
176
            if ($institutionAuthorization->institutionRole === $role) {
177
                $entityManager->persist($institutionAuthorization);
178
            }
179
        }
180
    }
181
}
182