Completed
Push — feature/authz-service ( 007e40...3c7a6c )
by
unknown
02:53
created

InstitutionAuthorizationRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 20
loc 105
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A findAuthorizationOptionsForInstitution() 10 10 1
A findInstitutionByAuthorization() 10 10 1
A saveInstitutionOption() 0 15 2
A save() 0 9 1
A clearOldAuthorizations() 0 12 1
A addNewAuthorizations() 0 8 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
    public function findAuthorizationOptionsForInstitution(Institution $institution, 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...
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 View Code Duplication
    public function findInstitutionByAuthorization(Institution $institution, 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...
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
        $t = $entityManager->createQuery(
0 ignored issues
show
Unused Code introduced by
$t is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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
        $entityManager->flush();
117
    }
118
119
    /**
120
     * @param EntityManager $entityManager
121
     * @param InstitutionRole $role
122
     * @param InstitutionAuthorization[] $institutionAuthorizations
123
     */
124
    private function addNewAuthorizations(EntityManager $entityManager, InstitutionRole $role, array $institutionAuthorizations)
125
    {
126
        foreach ($institutionAuthorizations as $institutionAuthorization) {
127
            if ($institutionAuthorization->institutionRole === $role) {
128
                $entityManager->persist($institutionAuthorization);
129
            }
130
        }
131
    }
132
}
133