Completed
Push — master ( e111ee...b6ca56 )
by Artem
04:57
created

UnauthorizedRequest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 0
loc 58
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B onResponse() 0 21 5
1
<?php
2
3
//----------------------------------------------------------------------
4
//
5
//  Copyright (C) 2017 Artem Rodygin
6
//
7
//  You should have received a copy of the MIT License along with
8
//  this file. If not, see <http://opensource.org/licenses/MIT>.
9
//
10
//----------------------------------------------------------------------
11
12
namespace Pignus\EventListener;
13
14
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
17
use Symfony\Component\Routing\RouterInterface;
18
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
19
use Symfony\Component\Translation\TranslatorInterface;
20
21
/**
22
 * Provides special response in case of unauthorized AJAX request.
23
 */
24
class UnauthorizedRequest
25
{
26
    protected $router;
27
    protected $translator;
28
    protected $utils;
29
    protected $firewalls;
30
    protected $routes;
31
32
    /**
33
     * Dependency Injection constructor.
34
     *
35
     * @param RouterInterface     $router
36
     * @param TranslatorInterface $translator
37
     * @param AuthenticationUtils $utils
38
     * @param FirewallMap         $firewalls
39
     * @param string[]            $routes
40
     */
41 4
    public function __construct(
42
        RouterInterface     $router,
43
        TranslatorInterface $translator,
44
        AuthenticationUtils $utils,
45
        FirewallMap         $firewalls,
46
        array               $routes)
47
    {
48 4
        $this->router     = $router;
49 4
        $this->translator = $translator;
50 4
        $this->utils      = $utils;
51 4
        $this->firewalls  = $firewalls;
52 4
        $this->routes     = $routes;
53 4
    }
54
55
    /**
56
     * Overrides the response if user is redirected to login page and it was an AJAX request.
57
     *
58
     * @param FilterResponseEvent $event
59
     */
60 4
    public function onResponse(FilterResponseEvent $event)
61
    {
62 4
        $request  = $event->getRequest();
63 4
        $response = $event->getResponse();
64
65 4
        $firewall = $this->firewalls->getFirewallConfig($request)->getName();
66
67 4
        if (!array_key_exists($firewall, $this->routes)) {
68 1
            return;
69
        }
70
71 3
        $url = $this->router->generate($this->routes[$firewall], [], RouterInterface::ABSOLUTE_URL);
72
73 3
        if ($request->isXmlHttpRequest() && $response->isRedirect($url)) {
74
75 1
            $error   = $this->utils->getLastAuthenticationError();
76 1
            $message = $this->translator->trans($error === null ? 'Authentication required.' : $error->getMessage());
77
78 1
            $event->setResponse(new Response($message, Response::HTTP_UNAUTHORIZED));
79
        }
80 3
    }
81
}
82