Completed
Push — bugfix/enforce_raa ( 1bb496...841cd0 )
by
unknown
02:14
created

InstitutionListingRepository::getInstitutionsForSelectRaaAsSraa()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
            ->innerJoin(
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 InstitutionRole $role
97
     * @return array
98
     */
99 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...
100
    {
101
        switch (true) {
102
            case $role->equals(InstitutionRole::useRa()):
103
                return ['use_ra'];
104
            case $role->equals(InstitutionRole::useRaa()):
105
                return ['use_raa'];
106
            default:
107
                return [];
108
        }
109
    }
110
111
    /**
112
     * @param InstitutionRole $role
113
     * @return array
114
     */
115 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...
116
    {
117
        switch (true) {
118
            case $role->equals(InstitutionRole::useRa()):
119
                return ['ra', 'raa'];
120
            case $role->equals(InstitutionRole::useRaa()):
121
                return ['raa'];
122
            default:
123
                return [];
124
        }
125
    }
126
}
127