1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Eole\Sandstone\Push\Debug\DataCollector; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
8
|
|
|
use Eole\Sandstone\Push\Debug\TraceablePushServerInterface; |
9
|
|
|
use Eole\Sandstone\Push\PushServerInterface; |
10
|
|
|
|
11
|
|
|
class PushServerDataCollector extends DataCollector |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
const NAME = 'sandstone.push_server'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var PushServerInterface |
20
|
|
|
*/ |
21
|
|
|
private $pushServer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param PushServerInterface $pushServer |
25
|
|
|
*/ |
26
|
|
|
public function __construct(PushServerInterface $pushServer) |
27
|
|
|
{ |
28
|
|
|
$this->pushServer = $pushServer; |
29
|
|
|
$this->updateMessages(array()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@InheritDoc} |
34
|
|
|
*/ |
35
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
36
|
|
|
{ |
37
|
|
|
if ($this->pushServer instanceof TraceablePushServerInterface) { |
38
|
|
|
$this->updateMessages($this->pushServer->getSentMessages()); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Update data collector data with messages. |
44
|
|
|
* |
45
|
|
|
* @param string[] $messages |
46
|
|
|
*/ |
47
|
|
|
private function updateMessages(array $messages) |
48
|
|
|
{ |
49
|
|
|
$this->reset(); |
50
|
|
|
|
51
|
|
|
foreach ($messages as $message) { |
52
|
|
|
$messageSize = strlen($message); |
53
|
|
|
|
54
|
|
|
$this->data['messages_size'] += $messageSize; |
55
|
|
|
$this->data['messages_count']++; |
56
|
|
|
|
57
|
|
|
$this->data['messages'] []= array( |
58
|
|
|
'content' => $message, |
59
|
|
|
'size' => $messageSize, |
60
|
|
|
'decoded' => unserialize($message), |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getData() |
69
|
|
|
{ |
70
|
|
|
return $this->data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@InheritDoc} |
75
|
|
|
*/ |
76
|
|
|
public function getName() |
77
|
|
|
{ |
78
|
|
|
return self::NAME; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@InheritDoc} |
83
|
|
|
*/ |
84
|
|
|
public function reset() |
85
|
|
|
{ |
86
|
|
|
$this->data = array( |
87
|
|
|
'messages_size' => 0, |
88
|
|
|
'messages_count' => 0, |
89
|
|
|
'messages' => array(), |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|