Completed
Push — symfony-3.3 ( 4cbc43...e8b928 )
by Kamil
23:34
created

AuthenticationSuccessHandler::determineTargetUrl()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 6.6037
cc 8
eloc 11
nc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\UserBundle\Authentication;
13
14
use Symfony\Component\HttpFoundation\JsonResponse;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
17
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
18
use Symfony\Component\Security\Http\ParameterBagUtils;
19
use Symfony\Component\Security\Http\Util\TargetPathTrait;
20
21
/**
22
 * @author Arkadiusz Krakowiak <[email protected]>
23
 */
24
final class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler
25
{
26
    use TargetPathTrait;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
32
    {
33
        if ($request->isXmlHttpRequest()) {
34
            return new JsonResponse(['success' => true, 'username' => $token->getUsername()]);
35
        }
36
37
        return parent::onAuthenticationSuccess($request, $token);
38
    }
39
40
41
    /**
42
     * TODO: Workaround for regression introduced in Symfony 3.3. To be deleted when fixed.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
43
     *
44
     * @see https://github.com/symfony/symfony/pull/23411
45
     * @see https://github.com/symfony/symfony/pull/23061
46
     *
47
     * @internal
48
     *
49
     * {@inheritdoc}
50
     */
51
    protected function determineTargetUrl(Request $request)
52
    {
53
        if ($this->options['always_use_default_target_path']) {
54
            return $this->options['default_target_path'];
55
        }
56
57
        if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) {
58
            return $targetUrl;
59
        }
60
61
        if (null !== $this->providerKey && $targetUrl = $this->getTargetPath($request->getSession(), $this->providerKey)) {
0 ignored issues
show
Bug introduced by
It seems like $request->getSession() can be null; however, getTargetPath() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
62
            $this->removeTargetPath($request->getSession(), $this->providerKey);
0 ignored issues
show
Bug introduced by
It seems like $request->getSession() can be null; however, removeTargetPath() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
63
64
            return $targetUrl;
65
        }
66
67
        if ($this->options['use_referer'] && ($targetUrl = $request->headers->get('Referer')) && parse_url($targetUrl, PHP_URL_PATH) !== parse_url($this->httpUtils->generateUri($request, $this->options['login_path']), PHP_URL_PATH)) {
68
            return $targetUrl;
69
        }
70
71
        return $this->options['default_target_path'];
72
    }
73
}
74