RaaController::institutionConfigurationAction()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 52

Duplication

Lines 19
Ratio 36.54 %

Importance

Changes 0
Metric Value
dl 19
loc 52
rs 8.1138
c 0
b 0
f 0
cc 7
nc 12
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 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\Service\InstitutionListingService;
23
use Surfnet\StepupRa\RaBundle\Service\ProfileService;
24
use Surfnet\StepupRa\RaBundle\Command\SelectInstitutionCommand;
25
use Surfnet\StepupRa\RaBundle\Form\Type\SelectInstitutionType;
26
use Surfnet\StepupRa\RaBundle\Service\InstitutionConfigurationOptionsService;
27
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
28
use Symfony\Component\HttpFoundation\Request;
29
30
class RaaController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
31
{
32
    public function institutionConfigurationAction(Request $request)
33
    {
34
        $this->denyAccessUnlessGranted(['ROLE_RAA', 'ROLE_SRAA']);
35
36
        $logger = $this->get('logger');
37
        /** @var Identity $identity */
38
        $identity = $this->getUser();
39
40
        $profile = $this->getProfileService()->findByIdentityId($identity->id);
41
42 View Code Duplication
        if ($this->isGranted('ROLE_SRAA')) {
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...
43
            $institution = $identity->institution;
44
            $choices = $this->getInstitutionListingService()->getAll();
45
        } else {
46
            $choices = $profile->getRaaInstitutions();
47
            $institution = reset($choices);
48
        }
49
50
        // Only show the form if more than one institutions where found.
51 View Code Duplication
        if (count($choices) > 1) {
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...
52
            $command = new SelectInstitutionCommand();
53
            $command->institution = $institution;
54
            $command->availableInstitutions = $choices;
55
56
            $form = $this->createForm(SelectInstitutionType::class, $command);
57
            $form->handleRequest($request);
58
59
            if ($form->isSubmitted() && $form->isValid()) {
60
                $institution = $command->institution;
61
            }
62
        }
63
64
        $logger->notice(sprintf('Opening the institution configuration for "%s"', $institution));
65
66
        // Load the configuration for the institution that was selected.
67
        $configuration = $this->getInstitutionConfigurationOptionsService()
68
            ->getInstitutionConfigurationOptionsFor($institution);
69
70
        if (!$configuration) {
71
            $logger->warning(sprintf('Unable to find the institution configuration for "%s"', $institution));
72
            return $this->createNotFoundException('The institution configuration could not be found');
73
        }
74
75
        return $this->render(
76
            '@SurfnetStepupRaRa/institution_configuration/overview.html.twig',
77
            [
78
                'configuration' => (array)$configuration,
79
                'form' => isset($form) ? $form->createView() : null,
80
                'institution' => $institution,
81
            ]
82
        );
83
    }
84
85
    /**
86
     * @return InstitutionConfigurationOptionsService
87
     */
88
    private function getInstitutionConfigurationOptionsService()
89
    {
90
        return $this->get('ra.service.institution_configuration_options');
91
    }
92
93
    /**
94
     * @return ProfileService
95
     */
96
    private function getProfileService()
97
    {
98
        return $this->get('ra.service.profile');
99
    }
100
101
    /**
102
     * @return InstitutionListingService
103
     */
104
    private function getInstitutionListingService()
105
    {
106
        return $this->get('ra.service.institution_listing');
107
    }
108
}
109