Completed
Push — develop ( 784583...9a84aa )
by Michiel
02:05 queued 10s
created

RaaController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 24.29 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 17
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A selectInstitutionAction() 17 51 3
A getRaListingService() 0 4 1
A getInstitutionConfigurationOptionsService() 0 4 1

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\StepupRa\RaBundle\Controller;
20
21
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity;
22
use Surfnet\StepupRa\RaBundle\Command\ChangeRaaInstitutionCommand;
23
use Surfnet\StepupRa\RaBundle\Form\Type\RaaInstitutionSelectionType;
24
use Surfnet\StepupRa\RaBundle\Security\Authentication\Token\SamlToken;
25
use Surfnet\StepupRa\RaBundle\Security\Authorization\Voter\AllowedToSwitchInstitutionVoter;
26
use Surfnet\StepupRa\RaBundle\Service\InstitutionConfigurationOptionsService;
27
use Surfnet\StepupRa\RaBundle\Service\RaListingService;
28
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
29
use Symfony\Component\HttpFoundation\Request;
30
31
class RaaController extends Controller
32
{
33
    public function selectInstitutionAction(Request $request)
34
    {
35
        $this->denyAccessUnlessGranted([AllowedToSwitchInstitutionVoter::RAA_SWITCHING]);
36
37
        /** @var SamlToken $token */
38
        $token  = $this->get('security.token_storage')->getToken();
39
        $logger = $this->get('logger');
40
41
        /** @var Identity $identity */
42
        $identity = $token->getUser();
43
        $institution = $identity->institution;
44
45
        $logger->notice(sprintf('Select Institution for RAA "%s"', $identity->id));
46
47
        $raaSwitcherOptions = $this
48
            ->getRaListingService()
49
            ->createChoiceListFor($identity->id, $token->getIdentityInstitution());
50
51
        $command = new ChangeRaaInstitutionCommand();
52
        $command->institution = $institution;
53
        $command->availableInstitutions = $raaSwitcherOptions;
54
55
        $form = $this->createForm(RaaInstitutionSelectionType::class, $command);
56
        $form->handleRequest($request);
57
58 View Code Duplication
        if ($form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
59
            $institutionConfigurationOptions = $this->getInstitutionConfigurationOptionsService()
60
                ->getInstitutionConfigurationOptionsFor($command->institution);
61
            $token->changeInstitutionScope($command->institution, $institutionConfigurationOptions);
0 ignored issues
show
Bug introduced by
It seems like $institutionConfigurationOptions defined by $this->getInstitutionCon...($command->institution) on line 59 can be null; however, Surfnet\StepupRa\RaBundl...hangeInstitutionScope() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
62
63
            $flashMessage = $this->get('translator')
64
                ->trans('ra.sraa.changed_institution', ['%institution%' => $command->institution]);
65
            $this->get('session')->getFlashBag()->add('success', $flashMessage);
66
67
            $logger->notice(sprintf(
68
                'RAA "%s" successfully switched to institution "%s"',
69
                $identity->id,
70
                $command->institution
71
            ));
72
73
            return $this->redirect($this->generateUrl('ra_vetting_search'));
74
        }
75
76
        $logger->notice(sprintf('Showing select institution form for RAA "%s"', $identity->id));
77
78
        return $this->render(
79
            // Reuse the SRAA switcher twig template
80
            'SurfnetStepupRaRaBundle:Sraa:selectInstitution.html.twig',
81
            ['form' => $form->createView()]
82
        );
83
    }
84
85
    /**
86
     * @return RaListingService
87
     */
88
    private function getRaListingService()
89
    {
90
        return $this->get('ra.service.ra_listing');
91
    }
92
93
    /**
94
     * @return InstitutionConfigurationOptionsService
95
     */
96
    private function getInstitutionConfigurationOptionsService()
97
    {
98
        return $this->get('ra.service.institution_configuration_options');
99
    }
100
}
101