Completed
Pull Request — develop (#107)
by
unknown
05:31
created

LocaleController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 11
dl 0
loc 88
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A switchLocaleAction() 0 46 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\Security\Guard;
23
use OpenConext\ProfileBundle\Service\UserService;
24
use Psr\Log\LoggerInterface;
25
use Symfony\Component\Form\FormFactoryInterface;
26
use Symfony\Component\HttpFoundation\RedirectResponse;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
29
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
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 Guard
50
     */
51
    private $guard;
52
53
    /**
54
     * @var LoggerInterface
55
     */
56
    private $logger;
57
58
    public function __construct(
59
        FormFactoryInterface $formFactory,
60
        UserService $userService,
61
        FlashBagInterface $flashBag,
62
        Guard $guard,
63
        LoggerInterface $logger
64
    ) {
65
        $this->formFactory = $formFactory;
66
        $this->userService = $userService;
67
        $this->flashBag    = $flashBag;
68
        $this->guard       = $guard;
69
        $this->logger      = $logger;
70
    }
71
72
    public function switchLocaleAction(Request $request)
73
    {
74
        $this->guard->userIsLoggedIn();
75
76
        $this->logger->notice('User requested to switch locale');
77
78
        $returnUrl = $request->query->get('return-url');
79
80
        // Return URLs generated by us always include a path (ie. at least a forward slash)
81
        // @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L878
82
        $domain = $request->getSchemeAndHttpHost() . '/';
83
84
        if (strpos($returnUrl, $domain) !== 0) {
85
            $this->logger->error(sprintf(
86
                'Illegal return-url ("%s") for redirection after changing locale, aborting request',
87
                $returnUrl
88
            ));
89
            throw new BadRequestHttpException('Invalid return-url given');
90
        }
91
92
        $command = new ChangeLocaleCommand();
93
        $form = $this->formFactory->create('profile_switch_locale', $command, [])->handleRequest($request);
94
95
        $this->logger->notice(sprintf(
96
            'Switching locale from "%s" to "%s"',
97
            $request->getLocale(),
98
            $command->newLocale
99
        ));
100
101
        if ($form->isValid()) {
102
            $this->userService->changeLocale($command);
103
            $this->flashBag->add('success', 'profile.locale.locale_change_success');
104
105
            $this->logger->notice(sprintf(
106
                'Successfully switched locale from "%s" to "%s"',
107
                $request->getLocale(),
108
                $command->newLocale
109
            ));
110
        } else {
111
            $this->flashBag->add('error', 'profile.locale.locale_change_fail');
112
113
            $this->logger->error('Locale not switched: the switch locale form contained invalid data');
114
        }
115
116
        return new RedirectResponse($returnUrl);
117
    }
118
}
119