Passed
Pull Request — main (#308)
by Michiel
14:02 queued 06:58
created

LocaleController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
c 0
b 0
f 0
dl 0
loc 67
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B switchLocale() 0 55 6
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2014 SURFnet bv
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
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...
20
21
namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller;
22
23
use Psr\Log\LoggerInterface;
24
use Surfnet\StepupBundle\Command\SwitchLocaleCommand;
25
use Surfnet\StepupBundle\Form\Type\SwitchLocaleType;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Service\IdentityService;
27
use Surfnet\StepupSelfService\SelfServiceBundle\Service\InstitutionConfigurationOptionsService;
28
use Symfony\Component\HttpFoundation\RedirectResponse;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
31
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
32
use Symfony\Component\Routing\Attribute\Route;
33
use Symfony\Contracts\Translation\TranslatorInterface;
34
35
final class LocaleController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class LocaleController
Loading history...
36
{
37
    
38
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
39
        private readonly LoggerInterface $logger,
40
        InstitutionConfigurationOptionsService $configurationOptionsService,
41
        private readonly TranslatorInterface $translator,
42
        private readonly IdentityService $identityService,
43
    ) {
44
        parent::__construct($logger, $configurationOptionsService);
45
    }
46
47
    #[Route(
48
        path: '/switch-locale',
49
        name: 'ss_switch_locale',
50
        requirements: ['return-url' => '.+'],
51
        methods: ['POST']
52
    )]
53
    public function switchLocale(Request $request): RedirectResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function switchLocale()
Loading history...
54
    {
55
        $returnUrl = $request->query->get('return-url');
56
57
        // Return URLs generated by us always include a path (ie. at least a forward slash)
58
        // @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L878
59
        $domain = $request->getSchemeAndHttpHost() . '/';
60
        if (!str_starts_with($returnUrl, $domain)) {
61
            $this->logger->error(sprintf(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
62
                'Identity "%s" used illegal return-url for redirection after changing locale, aborting request',
63
                $this->getIdentity()->id
64
            ));
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...
65
66
            throw new BadRequestHttpException('Invalid return-url given');
67
        }
68
69
        $this->logger->info('Switching locale...');
70
71
        $identity = $this->getIdentity();
72
        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...
73
            throw new AccessDeniedHttpException('Cannot switch locales when not authenticated');
74
        }
75
76
        $command = new SwitchLocaleCommand();
77
        $command->identityId = $identity->id;
78
79
        $form = $this->createForm(
80
            SwitchLocaleType::class,
81
            $command,
82
            ['route' => 'ss_switch_locale', 'route_parameters' => ['return_url' => $returnUrl]]
83
        );
84
        $form->handleRequest($request);
85
86
        if (!$form->isSubmitted() || !$form->isValid()) {
87
            $this->addFlash('error', $this->translator->trans('ss.flash.invalid_switch_locale_form'));
88
            $this->logger->error('The switch locale form unexpectedly contained invalid data');
89
            return $this->redirect($returnUrl);
90
        }
91
92
        if (!$this->identityService->switchLocale($command)) {
93
            $this->addFlash('error', $this->translator->trans('ss.flash.error_while_switching_locale'));
94
            $this->logger->error('An error occurred while switching locales');
95
            return $this->redirect($returnUrl);
96
        }
97
98
        $this->logger->info('Successfully switched locale');
99
        $request->setLocale('nl_NL');
100
101
        return $this->redirect($returnUrl);
102
    }
103
}
104