Completed
Push — feature/authz-service ( 2625fb...8f3145 )
by
unknown
02:12
created

findAuthorizationOptionsForInstitutionByRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
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
    public function findAuthorizationOptionsForInstitutionByRole(Institution $institution, InstitutionRole $role)
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
     * @param InstitutionRole $role
0 ignored issues
show
Bug introduced by
There is no parameter named $role. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
50
     * @return InstitutionAuthorization[]
51
     */
52
    public function findAuthorizationOptionsForInstitution(Institution $institution)
53
    {
54
        return $this->createQueryBuilder('ia')
55
            ->where('ia.institution = :institution')
56
            ->setParameter('institution', $institution->getInstitution())
57
            ->getQuery()
58
            ->getResult();
59
    }
60
61
    /**
62
     * @param Institution $institution
63
     * @param InstitutionAuthorizationOption $institutionOption
64
     * @throws \Doctrine\ORM\OptimisticLockException
65
     */
66
    public function saveInstitutionOption(Institution $institution, InstitutionAuthorizationOption $institutionOption)
67
    {
68
        $institutionAuthorizations = [];
69
70
        foreach ($institutionOption->getInstitutions($institution) as $relatedInstitution) {
71
            $institutionAuthorizations[] = InstitutionAuthorization::create(
72
                $institution,
73
                $relatedInstitution,
74
                $institutionOption->getInstitutionRole()
75
            );
76
        }
77
78
        $this->save($institution, $institutionOption->getInstitutionRole(), $institutionAuthorizations);
79
    }
80
81
    /**
82
     * @param Institution $institution
83
     * @param InstitutionRole $role
84
     * @param InstitutionAuthorization[] $institutionAuthorizations
85
     * @throws \Doctrine\ORM\OptimisticLockException
86
     */
87
    private function save(Institution $institution, InstitutionRole $role, array $institutionAuthorizations)
88
    {
89
        $entityManager = $this->getEntityManager();
90
91
        $this->clearOldAuthorizations($entityManager, $institution, $role);
92
        $this->addNewAuthorizations($entityManager, $role, $institutionAuthorizations);
93
94
        $entityManager->flush();
95
    }
96
97
    /**
98
     * @param EntityManager $entityManager
99
     * @param Institution $institution
100
     * @param InstitutionRole $role
101
     */
102
    private function clearOldAuthorizations(EntityManager $entityManager, Institution $institution, InstitutionRole $role)
103
    {
104
        $entityManager->createQuery(
105
            'DELETE '.InstitutionAuthorization::class.' ia
106
            WHERE ia.institutionRole = :role AND ia.institution = :institution'
107
        )
108
            ->setParameter('role', $role)
109
            ->setParameter('institution', $institution->getInstitution())
110
            ->execute();
111
    }
112
113
    /**
114
     * @param EntityManager $entityManager
115
     * @param InstitutionRole $role
116
     * @param InstitutionAuthorization[] $institutionAuthorizations
117
     */
118
    private function addNewAuthorizations(EntityManager $entityManager, InstitutionRole $role, array $institutionAuthorizations)
119
    {
120
        foreach ($institutionAuthorizations as $institutionAuthorization) {
121
            if ($institutionAuthorization->institutionRole === $role) {
122
                $entityManager->persist($institutionAuthorization);
123
            }
124
        }
125
    }
126
}
127