1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Analytics Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2017 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2017 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\AnalyticsBundle\EventListener; |
18
|
|
|
|
19
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
22
|
|
|
use Symfony\Component\HttpKernel\Event\FinishRequestEvent; |
23
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
24
|
|
|
use Symfony\Component\HttpKernel\Event\PostResponseEvent; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class AnalyticsEventListener. |
28
|
|
|
*/ |
29
|
|
|
class AnalyticsEventListener |
30
|
|
|
{ |
31
|
|
|
const TERMINATE_IMIDEDIATELY = 'terminate-imidediately'; |
32
|
|
|
|
33
|
|
|
const EVENT_ENDPOINT = '_swp_analytics'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ProducerInterface |
37
|
|
|
*/ |
38
|
|
|
protected $producer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* AnalyticsEventListener constructor. |
42
|
|
|
* |
43
|
|
|
* @param ProducerInterface $producer |
44
|
|
|
*/ |
45
|
|
|
public function __construct(ProducerInterface $producer) |
46
|
|
|
{ |
47
|
|
|
$this->producer = $producer; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param GetResponseEvent $event |
52
|
|
|
*/ |
53
|
|
|
public function onKernelRequest(GetResponseEvent $event) |
54
|
|
|
{ |
55
|
|
|
$request = $event->getRequest(); |
56
|
|
|
if (strpos($request->getPathInfo(), self::EVENT_ENDPOINT)) { |
57
|
|
|
$this->producer->publish(serialize($request)); |
58
|
|
|
|
59
|
|
|
$response = new Response(); |
60
|
|
|
$response->headers->add(['terminate-imidediately' => true]); |
61
|
|
|
$event->setResponse($response); |
62
|
|
|
$event->stopPropagation(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param FilterResponseEvent $event |
68
|
|
|
*/ |
69
|
|
|
public function onKernelResponse(FilterResponseEvent $event) |
70
|
|
|
{ |
71
|
|
|
$response = $event->getResponse(); |
72
|
|
|
|
73
|
|
|
if ($response->headers->has(self::TERMINATE_IMIDEDIATELY)) { |
74
|
|
|
$event->stopPropagation(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param FinishRequestEvent $event |
80
|
|
|
*/ |
81
|
|
|
public function onKernelFinishRequest(FinishRequestEvent $event) |
82
|
|
|
{ |
83
|
|
|
$request = $event->getRequest(); |
84
|
|
|
if (strpos($request->getPathInfo(), self::EVENT_ENDPOINT)) { |
85
|
|
|
$event->stopPropagation(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param PostResponseEvent $event |
91
|
|
|
*/ |
92
|
|
|
public function onKernelTerminate(PostResponseEvent $event) |
93
|
|
|
{ |
94
|
|
|
$response = $event->getResponse(); |
95
|
|
|
if ($response->headers->has(self::TERMINATE_IMIDEDIATELY)) { |
96
|
|
|
$event->stopPropagation(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|