Completed
Push — symfony-3.3 ( 61ed24...4cbc43 )
by Kamil
35:18 queued 11:41
created

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
namespace Sylius\Bundle\CoreBundle\Symfony;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler as BaseDefaultAuthenticationSuccessHandler;
7
use Symfony\Component\Security\Http\ParameterBagUtils;
8
use Symfony\Component\Security\Http\Util\TargetPathTrait;
9
10
/**
11
 * 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...
12
 *
13
 * @see https://github.com/symfony/symfony/pull/23411
14
 * @see https://github.com/symfony/symfony/pull/23061
15
 *
16
 * @internal
17
 */
18
final class DefaultAuthenticationSuccessHandler extends BaseDefaultAuthenticationSuccessHandler
19
{
20
    use TargetPathTrait;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function determineTargetUrl(Request $request)
26
    {
27
        if ($this->options['always_use_default_target_path']) {
28
            return $this->options['default_target_path'];
29
        }
30
31
        if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) {
32
            return $targetUrl;
33
        }
34
35
        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...
36
            $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...
37
38
            return $targetUrl;
39
        }
40
41
        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)) {
42
            return $targetUrl;
43
        }
44
45
        return $this->options['default_target_path'];
46
    }
47
}
48