Passed
Push — feature/symfony6-upgrade ( 166417...86e2b9 )
by Paul
06:45
created

LocaleController::switchLocale()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 57
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 35
nc 5
nop 1
dl 0
loc 57
rs 8.7377
c 0
b 0
f 0

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
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller;
20
21
use Psr\Log\LoggerInterface;
22
use Surfnet\StepupBundle\Command\SwitchLocaleCommand;
23
use Surfnet\StepupBundle\Form\Type\SwitchLocaleType;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
26
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
27
use Symfony\Component\Routing\Annotation\Route;
28
29
final class LocaleController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class LocaleController
Loading history...
30
{
31
    #[Route(
32
        path: '/switch-locale',
33
        name: 'ss_switch_locale',
34
        requirements: ['return-url' => '.+'],
35
        methods: ['POST']
36
    )]
37
    public function switchLocale(Request $request): \Symfony\Component\HttpFoundation\RedirectResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function switchLocale()
Loading history...
38
    {
39
        $returnUrl = $request->query->get('return-url');
40
41
        // Return URLs generated by us always include a path (ie. at least a forward slash)
42
        // @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L878
43
        $domain = $request->getSchemeAndHttpHost() . '/';
44
        if (!str_starts_with($returnUrl, $domain)) {
45
            $this->get('logger')->error(sprintf(
0 ignored issues
show
Bug introduced by
The method get() does not exist on Surfnet\StepupSelfServic...roller\LocaleController. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
            $this->/** @scrutinizer ignore-call */ 
46
                   get('logger')->error(sprintf(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
46
                'Identity "%s" used illegal return-url for redirection after changing locale, aborting request',
47
                $this->getIdentity()->id
48
            ));
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
49
50
            throw new BadRequestHttpException('Invalid return-url given');
51
        }
52
53
        /** @var LoggerInterface $logger */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
54
        $logger = $this->get('logger');
55
        $logger->info('Switching locale...');
56
57
        $identity = $this->getIdentity();
58
        if (!$identity) {
0 ignored issues
show
introduced by
$identity is of type Surfnet\StepupMiddleware...e\Identity\Dto\Identity, thus it always evaluated to true.
Loading history...
59
            throw new AccessDeniedHttpException('Cannot switch locales when not authenticated');
60
        }
61
62
        $command = new SwitchLocaleCommand();
63
        $command->identityId = $identity->id;
64
65
        $form = $this->createForm(
66
            SwitchLocaleType::class,
67
            $command,
68
            ['route' => 'ss_switch_locale', 'route_parameters' => ['return_url' => $returnUrl]]
69
        );
70
        $form->handleRequest($request);
71
72
        if (!$form->isSubmitted() || !$form->isValid()) {
73
            $this->addFlash('error', $this->get('translator')->trans('ss.flash.invalid_switch_locale_form'));
74
            $logger->error('The switch locale form unexpectedly contained invalid data');
75
            return $this->redirect($returnUrl);
76
        }
77
78
        $service = $this->get('self_service.service.identity');
79
        if (!$service->switchLocale($command)) {
80
            $this->addFlash('error', $this->get('translator')->trans('ss.flash.error_while_switching_locale'));
81
            $logger->error('An error occurred while switching locales');
82
            return $this->redirect($returnUrl);
83
        }
84
85
        $logger->info('Successfully switched locale');
86
87
        return $this->redirect($returnUrl);
88
    }
89
}
90