Completed
Push — master ( 76453f...1c4278 )
by Julien
06:31
created

PushServerDataCollector::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
10
class PushServerDataCollector extends DataCollector
11
{
12
    /**
13
     * @var string
14
     */
15
    const NAME = 'sandstone.push_server';
16
17
    /**
18
     * @var TraceablePushServerInterface
19
     */
20
    private $pushServer;
21
22
    /**
23
     * @param TraceablePushServerInterface $pushServer
24
     */
25
    public function __construct(TraceablePushServerInterface $pushServer)
26
    {
27
        $this->pushServer = $pushServer;
28
        $this->updateMessages(array());
29
    }
30
31
    /**
32
     * {@InheritDoc}
33
     */
34
    public function collect(Request $request, Response $response, \Exception $exception = null)
35
    {
36
        $this->updateMessages($this->pushServer->getSentMessages());
37
    }
38
39
    /**
40
     * Update data collector data with messages.
41
     *
42
     * @param string[] $messages
43
     */
44
    private function updateMessages(array $messages)
45
    {
46
        $this->data = array(
47
            'messages_size' => 0,
48
            'messages_count' => 0,
49
            'messages' => array(),
50
        );
51
52
        foreach ($messages as $message) {
53
            $messageSize = strlen($message);
54
55
            $this->data['messages_size'] += $messageSize;
56
            $this->data['messages_count']++;
57
58
            $this->data['messages'] []= array(
59
                'content' => $message,
60
                'size' => $messageSize,
61
                'decoded' => unserialize($message),
62
            );
63
        }
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function getData()
70
    {
71
        return $this->data;
72
    }
73
74
    /**
75
     * {@InheritDoc}
76
     */
77
    public function getName()
78
    {
79
        return self::NAME;
80
    }
81
}
82