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

LocaleController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 70
wmc 4
lcom 1
cbo 10
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B switchLocaleAction() 0 28 3
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
use Symfony\Component\Translation\TranslatorInterface;
30
31
class LocaleController
32
{
33
    /**
34
     * @var FormFactoryInterface
35
     */
36
    private $formFactory;
37
38
    /**
39
     * @var UserService
40
     */
41
    private $userService;
42
43
    /**
44
     * @var FlashBagInterface
45
     */
46
    private $flashBag;
47
48
    /**
49
     * @var TranslatorInterface
50
     */
51
    private $translator;
52
53
    /**
54
     * @var LoggerInterface
55
     */
56
    private $logger;
57
58
    public function __construct(
59
        FormFactoryInterface $formFactory,
60
        UserService $userService,
61
        FlashBagInterface $flashBag,
62
        TranslatorInterface $translator,
63
        LoggerInterface $logger
64
    ) {
65
        $this->formFactory = $formFactory;
66
        $this->userService = $userService;
67
        $this->flashBag    = $flashBag;
68
        $this->logger      = $logger;
69
        $this->translator  = $translator;
70
    }
71
72
    public function switchLocaleAction(Request $request)
73
    {
74
        $returnUrl = $request->query->get('return-url');
75
76
        // Return URLs generated by us always include a path (ie. at least a forward slash)
77
        // @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L878
78
        $domain = $request->getSchemeAndHttpHost() . '/';
79
80
        if (strpos($returnUrl, $domain) !== 0) {
81
            $this->logger->error(sprintf(
82
                'Illegal return-url for redirection after changing locale, aborting request'
83
            ));
84
            throw new BadRequestHttpException('Invalid return-url given');
85
        }
86
87
        $command = new ChangeLocaleCommand();
88
89
        $form = $this->formFactory->create('profile_switch_locale', $command, [])->handleRequest($request);
90
        if ($form->isValid()) {
91
            $this->userService->changeLocale($command);
92
93
            $this->flashBag->add('success', 'profile.locale.locale_change_success');
94
        } else {
95
            $this->flashBag->add('error', 'profile.locale.locale_change_fail');
96
        }
97
98
        return new RedirectResponse($returnUrl);
99
    }
100
}
101