Completed
Pull Request — 4.0 (#3859)
by k-yamamura
06:07
created

EccubeAuthenticationSuccessHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B determineTargetUrl() 0 31 11
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Security\Http\Authentication;
15
16
use Symfony\Component\HttpFoundation\Request;
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
class EccubeAuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler
22
{
23
    use TargetPathTrait;
24
25
    /**
26
     * Builds the target URL according to the defined options.
27
     *
28
     * @return string
29
     */
30
    protected function determineTargetUrl(Request $request)
31
    {
32
        if ($this->options['always_use_default_target_path']) {
33
            return $this->options['default_target_path'];
34
        }
35
36
        if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) {
37
            if (preg_match('/^https?:\\\\/i', $targetUrl)) {
38
                $targetUrl = '/';
39
            }
40
41
            return $targetUrl;
42
        }
43
44
        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...
45
            $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...
46
47
            return $targetUrl;
48
        }
49
50
        if ($this->options['use_referer'] && $targetUrl = $request->headers->get('Referer')) {
51
            if (false !== $pos = strpos($targetUrl, '?')) {
52
                $targetUrl = substr($targetUrl, 0, $pos);
53
            }
54
            if ($targetUrl && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) {
55
                return $targetUrl;
56
            }
57
        }
58
59
        return $this->options['default_target_path'];
60
    }
61
62
}
63