Completed
Push — master ( 92f352...217e01 )
by Joel
02:19
created

RequestResponseListener::isExcludedPrefix()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 12
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace RedirectionIO\Client\ProxySymfony\EventListener;
4
5
use RedirectionIO\Client\Sdk\Exception\ExceptionInterface;
6
use RedirectionIO\Client\Sdk\Client;
7
use RedirectionIO\Client\Sdk\HttpMessage\Request;
8
use RedirectionIO\Client\Sdk\HttpMessage\Response;
9
use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirectResponse;
10
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
11
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
12
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
13
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
14
15
class RequestResponseListener
16
{
17
    private $client;
18
    private $excludedPrefixes;
19
20
    public function __construct(Client $client, array $excludedPrefixes = [])
21
    {
22
        $this->client = $client;
23
        $this->excludedPrefixes = $excludedPrefixes;
24
    }
25
26
    public function onKernelRequest(GetResponseEvent $event)
27
    {
28
        if (!$event->isMasterRequest()) {
29
            return;
30
        }
31
32
        $request = $event->getRequest();
33
34
        if ($this->isExcludedPrefix($request->getPathInfo())) {
35
            return;
36
        }
37
38
        $response = $this->client->findRedirect($this->createSdkRequest($request));
39
        $request->attributes->set('redirectionio_response', $response);
40
41
        if (!$response) {
42
            return;
43
        }
44
45
        410 === $response->getStatusCode()
46
            ? $event->setResponse((new SymfonyResponse())->setStatusCode(410))
47
            : $event->setResponse(new SymfonyRedirectResponse($response->getLocation(), $response->getStatusCode()));
0 ignored issues
show
introduced by Quentin Brunet
The method getLocation() does not exist on RedirectionIO\Client\Sdk\HttpMessage\Response. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            : $event->setResponse(new SymfonyRedirectResponse($response->/** @scrutinizer ignore-call */ getLocation(), $response->getStatusCode()));
Loading history...
48
    }
49
50
    public function onKernelTerminate(PostResponseEvent $event)
51
    {
52
        if ($this->isExcludedPrefix($event->getRequest()->getPathInfo())) {
53
            return;
54
        }
55
56
        $response = $event->getRequest()->attributes->get('redirectionio_response');
57
58
        if (!$response) {
59
            $response = new Response($event->getResponse()->getStatusCode());
60
        }
61
62
        $request = $this->createSdkRequest($event->getRequest());
63
64
        try {
65
            $this->client->log($request, $response);
66
        } catch (ExceptionInterface $exception) {
67
            // do nothing
68
        }
69
    }
70
71
    private function createSdkRequest(SymfonyRequest $symfonyRequest)
72
    {
73
        return new Request(
74
            $symfonyRequest->getHttpHost(),
75
            $symfonyRequest->getPathInfo(),
76
            $symfonyRequest->headers->get('User-Agent'),
77
            $symfonyRequest->headers->get('Referer'),
78
            $symfonyRequest->getScheme()
79
        );
80
    }
81
82
    private function isExcludedPrefix($url): bool
83
    {
84
        foreach ($this->excludedPrefixes as $excludedPrefix) {
85
            if (0 === strpos($url, $excludedPrefix)) {
86
                return true;
87
            }
88
        }
89
90
        return false;
91
    }
92
}
93