Completed
Push — master ( 55e9f7...74c356 )
by A.
05:34 queued 02:39
created

SraaController::selectInstitutionAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 54
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 54
rs 9.6716
cc 2
eloc 31
nc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\StepupRa\RaBundle\Controller;
20
21
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity;
22
use Surfnet\StepupRa\RaBundle\Command\SelectInstitutionCommand;
23
use Surfnet\StepupRa\RaBundle\Security\Authentication\Token\SamlToken;
24
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
25
use Symfony\Component\HttpFoundation\Request;
26
27
class SraaController extends Controller
28
{
29
    public function selectInstitutionAction(Request $request)
30
    {
31
        $this->denyAccessUnlessGranted(['ROLE_SRAA']);
32
33
        $logger = $this->get('logger');
34
        /** @var Identity $identity */
35
        $identity = $this->get('security.token_storage')->getToken()->getUser();
36
37
        $logger->notice(sprintf('Select Institution for SRAA "%s"', $identity->id));
38
39
        $command = new SelectInstitutionCommand();
40
        $command->institution = $identity->institution;
41
42
        $form = $this->createForm('sraa_institution_select', $command);
43
        $form->handleRequest($request);
44
45
        if ($form->isValid()) {
46
            $newInstitution = $command->institution;
47
            $identity->institution = $newInstitution;
48
49
            /** @var SamlToken $token */
50
            $tokenStorage = $this->get('security.token_storage');
51
            $token        = $tokenStorage->getToken();
52
53
            $institutionConfigurationOptionsService = $this->get('ra.service.institution_configuration_options');
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $institutionConfigurationOptionsService exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
54
            $institutionConfigurationOptions = $institutionConfigurationOptionsService
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $institutionConfigurationOptions exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
55
                ->getInstitutionConfigurationOptionsFor($newInstitution);
56
57
            // The institution configuration options need to be updated to correctly acquire the context of the
58
            // selected institution
59
            $token->setInstitutionConfigurationOptions($institutionConfigurationOptions);
60
            $tokenStorage->setToken($token);
61
62
            $flashMessage = $this->get('translator')
63
                ->trans('ra.sraa.changed_institution', ['%institution%' => $newInstitution]);
64
65
            $this->get('session')->getFlashBag()->add('success', $flashMessage);
66
67
            $logger->notice(sprintf(
68
                'SRAA "%s successfully switched to institution "%s"',
69
                $identity->id,
70
                $newInstitution
71
            ));
72
73
            return $this->redirect($this->generateUrl('ra_vetting_search'));
74
        }
75
76
        $logger->notice(sprintf('Showing select institution form for SRAA "%s"', $identity->id));
77
78
        return $this->render(
79
            'SurfnetStepupRaRaBundle:Sraa:selectInstitution.html.twig',
80
            ['form' => $form->createView()]
81
        );
82
    }
83
}
84