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())); |
|
|
|
|
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
|
|
|
|