Completed
Pull Request — develop (#236)
by
unknown
06:38 queued 03:11
created

InstitutionAuthorizationRepository::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 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...
84
     * @throws \Doctrine\ORM\OptimisticLockException
85
     */
86
    public function clearInstitutionOption(Institution $institution)
87
    {
88
        $entityManager = $this->getEntityManager();
89
90
        $entityManager->createQuery(
91
            'DELETE '.InstitutionAuthorization::class.' ia
92
            WHERE ia.institution = :institution'
93
        )
94
            ->setParameter('institution', $institution->getInstitution())
95
            ->execute();
96
97
        $entityManager->flush();
98
    }
99
100
    /**
101
     * @param Institution $institution
102
     * @param InstitutionRole $role
103
     * @param InstitutionAuthorization[] $institutionAuthorizations
104
     * @throws \Doctrine\ORM\OptimisticLockException
105
     */
106
    private function save(Institution $institution, InstitutionRole $role, array $institutionAuthorizations)
107
    {
108
        $entityManager = $this->getEntityManager();
109
110
        $this->clearOldAuthorizations($entityManager, $institution, $role);
111
        $this->addNewAuthorizations($entityManager, $role, $institutionAuthorizations);
112
113
        $entityManager->flush();
114
    }
115
116
    /**
117
     * @param EntityManager $entityManager
118
     * @param Institution $institution
119
     * @param InstitutionRole $role
120
     */
121
    private function clearOldAuthorizations(EntityManager $entityManager, Institution $institution, InstitutionRole $role)
122
    {
123
        $entityManager->createQuery(
124
            'DELETE '.InstitutionAuthorization::class.' ia
125
            WHERE ia.institutionRole = :role AND ia.institution = :institution'
126
        )
127
            ->setParameter('role', $role)
128
            ->setParameter('institution', $institution->getInstitution())
129
            ->execute();
130
    }
131
132
    /**
133
     * @param EntityManager $entityManager
134
     * @param InstitutionRole $role
135
     * @param InstitutionAuthorization[] $institutionAuthorizations
136
     */
137
    private function addNewAuthorizations(EntityManager $entityManager, InstitutionRole $role, array $institutionAuthorizations)
138
    {
139
        foreach ($institutionAuthorizations as $institutionAuthorization) {
140
            if ($institutionAuthorization->institutionRole === $role) {
141
                $entityManager->persist($institutionAuthorization);
142
            }
143
        }
144
    }
145
}
146