Completed
Pull Request — develop (#269)
by
unknown
07:22 queued 03:28
created

getInstitutionsForSelectRaa()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
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\Identity\Repository;
20
21
use Doctrine\ORM\EntityRepository;
22
use Doctrine\ORM\Query\Expr\Join;
23
use Surfnet\Stepup\Configuration\Value\InstitutionRole;
24
use Surfnet\Stepup\Identity\Collection\InstitutionCollection;
25
use Surfnet\Stepup\Identity\Value\IdentityId;
26
use Surfnet\Stepup\Identity\Value\Institution;
27
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionAuthorization;
28
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\InstitutionListing;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing;
30
31
class InstitutionListingRepository extends EntityRepository
32
{
33
    public function save(InstitutionListing $institution)
34
    {
35
        $this->getEntityManager()->persist($institution);
36
        $this->getEntityManager()->flush();
37
    }
38
39
    public function addIfNotExists(Institution $institution)
40
    {
41
        $existsQuery = $this->createQueryBuilder('i')
42
            ->where('i.institution = :institution')
43
            ->setParameter('institution', (string) $institution)
44
            ->getQuery()
45
            ->getOneOrNullResult();
46
47
        if ($existsQuery) {
48
            return;
49
        }
50
51
        $listing = InstitutionListing::createFrom($institution);
52
53
        $this->save($listing);
54
    }
55
56
    /**
57
     * @param InstitutionRole $role
58
     * @param IdentityId $actorId
59
     * @return InstitutionCollection
60
     */
61
    public function getInstitutionsForRole(InstitutionRole $role, IdentityId $actorId)
62
    {
63
        $qb = $this->createQueryBuilder('i')
64
            ->select("a.institution")
65
            ->innerJoin(RaListing::class, 'r', Join::WITH, "i.institution = r.raInstitution")
66
            ->leftJoin(
67
                InstitutionAuthorization::class,
68
                'a',
69
                Join::WITH,
70
                "i.institution = a.institutionRelation AND a.institutionRole IN (:authorizationRoles)"
71
            )
72
            ->where("r.identityId = :identityId AND r.role IN(:roles)")
73
            ->groupBy("a.institution");
74
75
        $qb->setParameter('identityId', (string)$actorId);
76
        $qb->setParameter(
77
            'authorizationRoles',
78
            $this->getAuthorizationRolesForAuthorization($role)
79
        );
80
        $qb->setParameter(
81
            'roles',
82
            $this->getAuthorizationRolesForRa($role)
83
        );
84
85
        $institutions = $qb->getQuery()->getArrayResult();
86
87
        $result = new InstitutionCollection();
88
        foreach ($institutions as $institution) {
89
            $result->add(new Institution((string)$institution['institution']));
90
        }
91
92
        return $result;
93
    }
94
95
    /**
96
     * @param IdentityId $actorId
97
     * @return InstitutionCollection
98
     */
99
    public function getInstitutionsForSelectRaa(IdentityId $actorId)
100
    {
101
        $qb = $this->createQueryBuilder('i')
102
            ->select("a.institutionRelation")
103
            ->innerJoin(RaListing::class, 'r', Join::WITH, "i.institution = r.raInstitution")
104
            ->leftJoin(
105
                InstitutionAuthorization::class,
106
                'a',
107
                Join::WITH,
108
                "r.institution = a.institution AND a.institutionRole IN (:authorizationRoles)"
109
            )
110
            ->where("r.identityId = :identityId AND r.role IN(:roles)")
111
            ->groupBy("a.institutionRelation");
112
113
        $qb->setParameter('identityId', (string)$actorId);
114
        $qb->setParameter(
115
            'authorizationRoles',
116
            [InstitutionRole::ROLE_SELECT_RAA]
117
        );
118
        $qb->setParameter(
119
            'roles',
120
            ['raa']
121
        );
122
        $institutions = $qb->getQuery()->getArrayResult();
123
124
        $result = new InstitutionCollection();
125
        foreach ($institutions as $institution) {
126
            $result->add(new Institution((string)$institution['institutionRelation']));
127
        }
128
129
        return $result;
130
    }
131
132
133
    /**
134
     * @param Institution $institution
135
     * @return InstitutionCollection
136
     */
137
    public function getInstitutionsForSelectRaaAsSraa(Institution $institution)
138
    {
139
        $qb = $this->createQueryBuilder('i')
140
            ->select("a.institution")
141
            ->innerJoin(
142
                InstitutionAuthorization::class,
143
                'a',
144
                Join::WITH,
145
                "r.institution = a.institution AND a.institutionRole IN (:authorizationRoles)"
146
            )
147
            ->where("a.institution = :institution")
148
            ->groupBy("a.institution");
149
150
        $qb->setParameter('institution', (string)$institution);
151
        $qb->setParameter(
152
            'authorizationRoles',
153
            [InstitutionRole::ROLE_SELECT_RAA]
154
        );
155
156
        $institutions = $qb->getQuery()->getArrayResult();
157
158
        $result = new InstitutionCollection();
159
        foreach ($institutions as $institution) {
160
            $result->add(new Institution((string)$institution['institution']));
161
        }
162
163
        return $result;
164
    }
165
166
    /**
167
     * @param InstitutionRole $role
168
     * @return array
169
     */
170 View Code Duplication
    private function getAuthorizationRolesForAuthorization(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...
171
    {
172
        switch (true) {
173
            case $role->equals(InstitutionRole::useRa()):
174
                return ['use_ra'];
175
            case $role->equals(InstitutionRole::useRaa()):
176
                return ['use_raa'];
177
            default:
178
                return [];
179
        }
180
    }
181
182
    /**
183
     * @param InstitutionRole $role
184
     * @return array
185
     */
186 View Code Duplication
    private function getAuthorizationRolesForRa(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...
187
    {
188
        switch (true) {
189
            case $role->equals(InstitutionRole::useRa()):
190
                return ['ra', 'raa'];
191
            case $role->equals(InstitutionRole::useRaa()):
192
                return ['raa'];
193
            default:
194
                return [];
195
        }
196
    }
197
}
198