Completed
Pull Request — develop (#290)
by
unknown
05:53 queued 02:45
created

getAuthorizationRolesForAuthorization()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
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\EntityManager;
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\RaListing;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\AuthorityRole;
30
31
class AuthorizationRepository
32
{
33
    /**
34
     * @var EntityManager
35
     */
36
    private $entityManager;
37
38
    public function __construct(EntityManager $entityManager)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
39
    {
40
        $this->entityManager = $entityManager;
41
    }
42
43
    /**
44
     * @param InstitutionRole $role
45
     * @param IdentityId $actorId
46
     * @return InstitutionCollection
47
     */
48
    public function getInstitutionsForRole(InstitutionRole $role, IdentityId $actorId)
49
    {
50
        $qb = $this->entityManager->createQueryBuilder()
51
            ->select("a.institution")
52
            ->from(RaListing::class, 'i')
53
            ->innerJoin(RaListing::class, 'r', Join::WITH, "i.institution = r.raInstitution")
54
            ->innerJoin(
55
                InstitutionAuthorization::class,
56
                'a',
57
                Join::WITH,
58
                "i.institution = a.institutionRelation AND a.institutionRole IN (:authorizationRoles)"
59
            )
60
            ->where("r.identityId = :identityId AND r.role IN(:roles)")
61
            ->groupBy("a.institution");
62
63
        $qb->setParameter('identityId', (string)$actorId);
64
        $qb->setParameter(
65
            'authorizationRoles',
66
            $this->getAuthorizationRolesForAuthorization($role)
67
        );
68
        $qb->setParameter(
69
            'roles',
70
            $this->getAuthorizationRolesForRa($role)
71
        );
72
73
        $institutions = $qb->getQuery()->getArrayResult();
74
75
        $result = new InstitutionCollection();
76
        foreach ($institutions as $institution) {
77
            $result->add(new Institution((string)$institution['institution']));
78
        }
79
80
        return $result;
81
    }
82
83
    /**
84
     * @param InstitutionRole $role
85
     * @return array
86
     */
87 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...
88
    {
89
        switch (true) {
90
            case $role->equals(InstitutionRole::useRa()):
91
                return [InstitutionRole::ROLE_USE_RA];
92
            case $role->equals(InstitutionRole::useRaa()):
93
                return [InstitutionRole::ROLE_USE_RAA];
94
            default:
95
                return [];
96
        }
97
    }
98
99
    /**
100
     * @param InstitutionRole $role
101
     * @return array
102
     */
103 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...
104
    {
105
        switch (true) {
106
            case $role->equals(InstitutionRole::useRa()):
107
                return [AuthorityRole::ROLE_RA, AuthorityRole::ROLE_RAA];
108
            case $role->equals(InstitutionRole::useRaa()):
109
                return [AuthorityRole::ROLE_RAA];
110
            default:
111
                return [];
112
        }
113
    }
114
}
115