Completed
Pull Request — develop (#28)
by A.
05:07
created

LocaleController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
rs 9.4286
cc 1
eloc 9
nc 1
nop 4
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\Profile\Command\ChangeLocaleCommand;
22
use OpenConext\ProfileBundle\Service\UserService;
23
use Psr\Log\LoggerInterface;
24
use Symfony\Component\Form\FormFactoryInterface;
25
use Symfony\Component\HttpFoundation\RedirectResponse;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
28
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
29
30
class LocaleController
31
{
32
    /**
33
     * @var FormFactoryInterface
34
     */
35
    private $formFactory;
36
37
    /**
38
     * @var UserService
39
     */
40
    private $userService;
41
42
    /**
43
     * @var FlashBagInterface
44
     */
45
    private $flashBag;
46
47
    /**
48
     * @var LoggerInterface
49
     */
50
    private $logger;
51
52
    public function __construct(
53
        FormFactoryInterface $formFactory,
54
        UserService $userService,
55
        FlashBagInterface $flashBag,
56
        LoggerInterface $logger
57
    ) {
58
        $this->formFactory = $formFactory;
59
        $this->userService = $userService;
60
        $this->flashBag    = $flashBag;
61
        $this->logger      = $logger;
62
    }
63
64
    public function switchLocaleAction(Request $request)
65
    {
66
        $returnUrl = $request->query->get('return-url');
67
68
        // Return URLs generated by us always include a path (ie. at least a forward slash)
69
        // @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L878
70
        $domain = $request->getSchemeAndHttpHost() . '/';
71
72
        if (strpos($returnUrl, $domain) !== 0) {
73
            $this->logger->error(sprintf(
74
                'Illegal return-url for redirection after changing locale, aborting request'
75
            ));
76
            throw new BadRequestHttpException('Invalid return-url given');
77
        }
78
79
        $command = new ChangeLocaleCommand();
80
81
        $form = $this->formFactory->create('profile_switch_locale', $command, [])->handleRequest($request);
82
        if ($form->isValid()) {
83
            $this->userService->changeLocale($command);
84
85
            $this->flashBag->add('success', 'profile.locale.locale_change_success');
86
        } else {
87
            $this->flashBag->add('error', 'profile.locale.locale_change_fail');
88
        }
89
90
        return new RedirectResponse($returnUrl);
91
    }
92
}
93