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

EccubeAuthenticationFailureHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 13.33 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B onAuthenticationFailure() 6 34 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\HttpKernel\HttpKernelInterface;
18
use Symfony\Component\Security\Core\Exception\AuthenticationException;
19
use Symfony\Component\Security\Core\Security;
20
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
21
use Symfony\Component\Security\Http\ParameterBagUtils;
22
23
class EccubeAuthenticationFailureHandler extends DefaultAuthenticationFailureHandler
24
{
25
26
    /**
27
     * @param Request $request
28
     * @param AuthenticationException $exception
29
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
30
     * @throws \Exception
31
     */
32
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
33
    {
34
        if ($failureUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['failure_path_parameter'])) {
35
            if (preg_match('/^https?:\\\\/i', $failureUrl)) {
36
                $this->options['failure_path'] = '/';
37
            } else {
38
                $this->options['failure_path'] = $failureUrl;
39
            }
40
        }
41
42
        if (null === $this->options['failure_path']) {
43
            $this->options['failure_path'] = $this->options['login_path'];
44
        }
45
46
        if ($this->options['failure_forward']) {
47 View Code Duplication
            if (null !== $this->logger) {
48
                $this->logger->debug('Authentication failure, forward triggered.', array('failure_path' => $this->options['failure_path']));
49
            }
50
51
            $subRequest = $this->httpUtils->createRequest($request, $this->options['failure_path']);
52
            $subRequest->attributes->set(Security::AUTHENTICATION_ERROR, $exception);
53
54
            return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
55
        }
56
57 View Code Duplication
        if (null !== $this->logger) {
58
            $this->logger->debug('Authentication failure, redirect triggered.', array('failure_path' => $this->options['failure_path']));
59
        }
60
61
        $request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
62
63
64
        return $this->httpUtils->createRedirectResponse($request, $this->options['failure_path']);
65
    }
66
67
}
68