LocaleController::switchLocaleAction()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 9.1781
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * Copyright 2015 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 OpenConext\ProfileBundle\Controller;
20
21
use OpenConext\ProfileBundle\Form\Type\SwitchLocaleType;
22
use OpenConext\ProfileBundle\Profile\Command\ChangeLocaleCommand;
23
use OpenConext\ProfileBundle\Security\Guard;
24
use OpenConext\ProfileBundle\Service\UserService;
25
use Psr\Log\LoggerInterface;
26
use Symfony\Component\Form\FormFactoryInterface;
27
use Symfony\Component\HttpFoundation\RedirectResponse;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
30
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
31
32
class LocaleController
33
{
34
    /**
35
     * @var FormFactoryInterface
36
     */
37
    private $formFactory;
38
39
    /**
40
     * @var UserService
41
     */
42
    private $userService;
43
44
    /**
45
     * @var FlashBagInterface
46
     */
47
    private $flashBag;
48
49
    /**
50
     * @var Guard
51
     */
52
    private $guard;
53
54
    /**
55
     * @var LoggerInterface
56
     */
57
    private $logger;
58
59
    public function __construct(
60
        FormFactoryInterface $formFactory,
61
        UserService $userService,
62
        FlashBagInterface $flashBag,
63
        Guard $guard,
64
        LoggerInterface $logger
65
    ) {
66
        $this->formFactory = $formFactory;
67
        $this->userService = $userService;
68
        $this->flashBag    = $flashBag;
69
        $this->guard       = $guard;
70
        $this->logger      = $logger;
71
    }
72
73
    public function switchLocaleAction(Request $request)
74
    {
75
        $this->guard->userIsLoggedIn();
76
77
        $this->logger->notice('User requested to switch locale');
78
79
        $returnUrl = $request->query->get('return-url');
80
81
        // Return URLs generated by us always include a path (ie. at least a forward slash)
82
        // @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L878
83
        $domain = $request->getSchemeAndHttpHost() . '/';
84
85
        if (strpos($returnUrl, $domain) !== 0) {
86
            $this->logger->error(sprintf(
87
                'Illegal return-url ("%s") for redirection after changing locale, aborting request',
88
                $returnUrl
89
            ));
90
            throw new BadRequestHttpException('Invalid return-url given');
91
        }
92
93
        $command = new ChangeLocaleCommand();
94
        $form = $this->formFactory->create(SwitchLocaleType::class, $command, [])->handleRequest($request);
95
96
        $this->logger->notice(sprintf(
97
            'Switching locale from "%s" to "%s"',
98
            $request->getLocale(),
99
            $command->newLocale
100
        ));
101
102
        if ($form->isValid()) {
103
            $this->userService->changeLocale($command);
104
            $this->flashBag->add('success', 'profile.locale.locale_change_success');
105
106
            $this->logger->notice(sprintf(
107
                'Successfully switched locale from "%s" to "%s"',
108
                $request->getLocale(),
109
                $command->newLocale
110
            ));
111
        } else {
112
            $this->flashBag->add('error', 'profile.locale.locale_change_fail');
113
114
            $this->logger->error('Locale not switched: the switch locale form contained invalid data');
115
        }
116
117
        return new RedirectResponse($returnUrl);
118
    }
119
}
120