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 SWP\Bundle\AnalyticsBundle\Messenger\AnalyticsEvent; |
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\PostResponseEvent; |
24
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
25
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
26
|
|
|
|
27
|
|
|
class AnalyticsEventListener |
28
|
|
|
{ |
29
|
|
|
const TERMINATE_IMMEDIATELY = 'terminate-immediately'; |
30
|
|
|
|
31
|
|
|
const EVENT_ENDPOINT = '_swp_analytics'; |
32
|
|
|
|
33
|
|
|
/** @var MessageBusInterface */ |
34
|
|
|
protected $messageBus; |
35
|
|
|
|
36
|
|
|
public function __construct(MessageBusInterface $messageBus) |
37
|
|
|
{ |
38
|
|
|
$this->messageBus = $messageBus; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function onKernelRequest(RequestEvent $event) |
42
|
|
|
{ |
43
|
|
|
$request = $event->getRequest(); |
44
|
|
|
if (strpos($request->getPathInfo(), self::EVENT_ENDPOINT) && |
45
|
|
|
in_array($request->getMethod(), ['POST', 'GET']) |
46
|
|
|
) { |
47
|
|
|
$httpReferrer = $request->server->get('HTTP_REFERER', $request->query->get('host', $request->getHost())); |
48
|
|
|
|
49
|
|
|
$this->messageBus->dispatch(new AnalyticsEvent( |
50
|
|
|
$httpReferrer, |
51
|
|
|
(int) $request->query->get('articleId', null), |
52
|
|
|
$request->query->get('ref', null) |
53
|
|
|
)); |
54
|
|
|
|
55
|
|
|
$response = new Response(); |
56
|
|
|
$response->headers->add([self::TERMINATE_IMMEDIATELY => true]); |
57
|
|
|
$event->setResponse($response); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function onKernelResponse(FilterResponseEvent $event) |
62
|
|
|
{ |
63
|
|
|
$response = $event->getResponse(); |
64
|
|
|
|
65
|
|
|
if ($response->headers->has(self::TERMINATE_IMMEDIATELY)) { |
66
|
|
|
$event->stopPropagation(); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function onKernelFinishRequest(FinishRequestEvent $event) |
71
|
|
|
{ |
72
|
|
|
$request = $event->getRequest(); |
73
|
|
|
if (strpos($request->getPathInfo(), self::EVENT_ENDPOINT)) { |
74
|
|
|
$event->stopPropagation(); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function onKernelTerminate(PostResponseEvent $event) |
79
|
|
|
{ |
80
|
|
|
$response = $event->getResponse(); |
81
|
|
|
if ($response->headers->has(self::TERMINATE_IMMEDIATELY)) { |
82
|
|
|
$event->stopPropagation(); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.