Passed
Push — feature/build-and-publish-test... ( c594c2...5abced )
by
unknown
18:44
created

LocaleController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B switchLocale() 0 55 6
A __construct() 0 7 1
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 Surfnet\StepupSelfService\SelfServiceBundle\Service\IdentityService;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Service\InstitutionConfigurationOptionsService;
26
use Symfony\Component\HttpFoundation\RedirectResponse;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
29
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
30
use Symfony\Component\Routing\Annotation\Route;
31
use Symfony\Contracts\Translation\TranslatorInterface;
32
33
final class LocaleController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class LocaleController
Loading history...
34
{
35
    
36
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
37
        private readonly LoggerInterface $logger,
38
        InstitutionConfigurationOptionsService $configurationOptionsService,
39
        private readonly TranslatorInterface $translator,
40
        private readonly IdentityService $identityService,
41
    ) {
42
        parent::__construct($logger, $configurationOptionsService);
43
    }
44
45
    #[Route(
46
        path: '/switch-locale',
47
        name: 'ss_switch_locale',
48
        requirements: ['return-url' => '.+'],
49
        methods: ['POST']
50
    )]
51
    public function switchLocale(Request $request): RedirectResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function switchLocale()
Loading history...
52
    {
53
        $returnUrl = $request->query->get('return-url');
54
55
        // Return URLs generated by us always include a path (ie. at least a forward slash)
56
        // @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L878
57
        $domain = $request->getSchemeAndHttpHost() . '/';
58
        if (!str_starts_with($returnUrl, $domain)) {
59
            $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...
60
                'Identity "%s" used illegal return-url for redirection after changing locale, aborting request',
61
                $this->getIdentity()->id
62
            ));
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...
63
64
            throw new BadRequestHttpException('Invalid return-url given');
65
        }
66
67
        $this->logger->info('Switching locale...');
68
69
        $identity = $this->getIdentity();
70
        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...
71
            throw new AccessDeniedHttpException('Cannot switch locales when not authenticated');
72
        }
73
74
        $command = new SwitchLocaleCommand();
75
        $command->identityId = $identity->id;
76
77
        $form = $this->createForm(
78
            SwitchLocaleType::class,
79
            $command,
80
            ['route' => 'ss_switch_locale', 'route_parameters' => ['return_url' => $returnUrl]]
81
        );
82
        $form->handleRequest($request);
83
84
        if (!$form->isSubmitted() || !$form->isValid()) {
85
            $this->addFlash('error', $this->translator->trans('ss.flash.invalid_switch_locale_form'));
86
            $this->logger->error('The switch locale form unexpectedly contained invalid data');
87
            return $this->redirect($returnUrl);
88
        }
89
90
91
        if (!$this->identityService->switchLocale($command)) {
92
            $this->addFlash('error', $this->translator->trans('ss.flash.error_while_switching_locale'));
93
            $this->logger->error('An error occurred while switching locales');
94
            return $this->redirect($returnUrl);
95
        }
96
97
        $this->logger->info('Successfully switched locale');
98
99
        return $this->redirect($returnUrl);
100
    }
101
}
102