Completed
Push — develop ( 75b66f...e97d4d )
by
unknown
02:23
created

saveInstitutionOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
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\InstitutionOption;
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 findAuthorizationOptionsForInstitution(Institution $institution, InstitutionRole $role)
37
    {
38
        return $this->createQueryBuilder('ia')
39
            ->where('ia.institution = :institution')
40
            ->andWhere('ia.type = :type')
41
            ->setParameter('institution', $institution->getInstitution())
42
            ->setParameter('institutionRole', $role)
43
            ->getQuery()
44
            ->getArrayResult();
45
    }
46
47
    /**
48
     * @param Institution $institution
49
     * @param InstitutionRole $role
50
     * @return InstitutionAuthorization|null
51
     * @throws \Doctrine\ORM\NonUniqueResultException
52
     */
53
    public function findInstitutionByAuthorization(Institution $institution, InstitutionRole $role)
54
    {
55
        return $this->createQueryBuilder('ia')
56
            ->where('ia.institutionRelation = :institution')
57
            ->andWhere('ia.type = :type')
58
            ->setParameter('institution', $institution->getInstitution())
59
            ->setParameter('institutionRole', $role)
60
            ->getQuery()
61
            ->getOneOrNullResult();
62
    }
63
64
    /**
65
     * @param Institution $institution
66
     * @param InstitutionOption $institutionOption
67
     * @throws \Doctrine\ORM\OptimisticLockException
68
     */
69
    public function saveInstitutionOption(Institution $institution, InstitutionOption $institutionOption)
70
    {
71
        $institutionAuthorizations = [];
72
73
        $institutionSet = $institutionOption->getInstitutionSet();
74
        foreach ($institutionSet->getInstitutions() as $relatedInstitution) {
75
            $institutionAuthorizations[] = InstitutionAuthorization::create(
76
                $institution,
77
                $relatedInstitution,
78
                $institutionOption->getInstitutionRole()
79
            );
80
        }
81
82
        $this->save($institution, $institutionOption->getInstitutionRole(), $institutionAuthorizations);
83
    }
84
85
    /**
86
     * @param Institution $institution
87
     * @param InstitutionRole $role
88
     * @param InstitutionAuthorization[] $institutionAuthorizations
89
     * @throws \Doctrine\ORM\OptimisticLockException
90
     */
91
    private function save(Institution $institution, InstitutionRole $role, array $institutionAuthorizations)
92
    {
93
        $entityManager = $this->getEntityManager();
94
95
        $this->clearOldAuthorizations($entityManager, $institution, $role);
96
        $this->addNewAuthorizations($entityManager, $role, $institutionAuthorizations);
97
98
        $entityManager->flush();
99
    }
100
101
    /**
102
     * @param EntityManager $entityManager
103
     * @param Institution $institution
104
     * @param InstitutionRole $role
105
     */
106
    private function clearOldAuthorizations(EntityManager $entityManager, Institution $institution, InstitutionRole $role)
107
    {
108
        $entityManager->createQuery(
109
            'DELETE '.InstitutionAuthorization::class.' ia
110
            WHERE ia.institutionRole = :role AND ia.institution = :institution'
111
        )
112
            ->setParameter('role', $role)
113
            ->setParameter('institution', $institution->getInstitution())
114
            ->execute();
115
    }
116
117
    /**
118
     * @param EntityManager $entityManager
119
     * @param InstitutionRole $role
120
     * @param InstitutionAuthorization[] $institutionAuthorizations
121
     */
122
    private function addNewAuthorizations(EntityManager $entityManager, InstitutionRole $role, array $institutionAuthorizations)
123
    {
124
        foreach ($institutionAuthorizations as $institutionAuthorization) {
125
            if ($institutionAuthorization->institutionRole === $role) {
126
                $entityManager->persist($institutionAuthorization);
127
            }
128
        }
129
    }
130
}
131