Completed
Push — master ( 670bd1...b0560b )
by Alessandro
02:58
created

MongoDbDataCollector   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 119
ccs 28
cts 40
cp 0.7
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setLogger() 0 4 1
A collect() 0 18 3
A getQueryCount() 0 4 1
A getQueries() 0 4 1
A getTime() 0 4 1
A getConnectionsCount() 0 4 1
A getConnections() 0 4 1
A getName() 0 4 1
B prepareUnserializableData() 0 18 5
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Facile\MongoDbBundle\DataCollector;
6
7
use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface;
8
use Facile\MongoDbBundle\Services\Loggers\Model\LogEvent;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
12
13
/**
14
 * Class MongoDbDataCollector.
15
 */
16
final class MongoDbDataCollector extends DataCollector
17
{
18
    const QUERY_KEYWORD = 'queries';
19
    const CONNECTION_KEYWORD = 'connections';
20
    const TIME_KEYWORD = 'totalTime';
21
22
    /** @var DataCollectorLoggerInterface */
23
    private $logger;
24
25 2
    public function __construct()
26
    {
27 2
        $this->data = [
28 2
            self::QUERY_KEYWORD => [],
29 2
            self::TIME_KEYWORD => 0.0,
30
            self::CONNECTION_KEYWORD => [],
31
        ];
32 2
    }
33
34
    /**
35
     * @param DataCollectorLoggerInterface $logger
36
     */
37 2
    public function setLogger(DataCollectorLoggerInterface $logger)
38
    {
39 2
        $this->logger = $logger;
40 2
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function collect(Request $request, Response $response, \Exception $exception = null)
46
    {
47 2
        while ($this->logger->hasLoggedEvents()) {
48
            /** @var LogEvent $event */
49 1
            $event = $this->logger->getLoggedEvent();
50
51
            // with extension version under 1.2.0 some Mongo objects can't be automatically serialized
52 1
            if (phpversion("mongodb") < "1.2.0") {
53
                $eventData = $this->prepareUnserializableData($event->getData());
54
                $event->setData($eventData);
55
            }
56
57 1
            $this->data[self::QUERY_KEYWORD][] = $event;
58 1
            $this->data[self::TIME_KEYWORD] += $event->getExecutionTime();
59
        }
60
61 2
        $this->data[self::CONNECTION_KEYWORD] = $this->logger->getConnections();
62 2
    }
63
64
    /**
65
     * @return int
66
     */
67 2
    public function getQueryCount(): int
68
    {
69 2
        return count($this->data[self::QUERY_KEYWORD]);
70
    }
71
72
    /**
73
     * @return array
74
     */
75 2
    public function getQueries(): array
76
    {
77 2
        return $this->data[self::QUERY_KEYWORD];
78
    }
79
80
    /**
81
     * @return float
82
     */
83 2
    public function getTime(): float
84
    {
85 2
        return (float)($this->data[self::TIME_KEYWORD] * 1000);
86
    }
87
88
    /**
89
     * @return int
90
     */
91 2
    public function getConnectionsCount(): int
92
    {
93 2
        return count($this->data[self::CONNECTION_KEYWORD]);
94
    }
95
    /**
96
     * @return array|string[]
97
     */
98 2
    public function getConnections(): array
99
    {
100 2
        return $this->data[self::CONNECTION_KEYWORD];
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 2
    public function getName()
107
    {
108 2
        return 'mongodb';
109
    }
110
111
    /**
112
     * @param array $data
113
     *
114
     * @return array
115
     */
116
    private function prepareUnserializableData(array $data): array
117
    {
118
        foreach ($data as $key => $item) {
119
            if (method_exists($item, 'getArrayCopy')) {
120
                $data[$key] = $this->prepareUnserializableData($item->getArrayCopy());
121
            }
122
123
            if (method_exists($item, '__toString')) {
124
                $data[$key] = $item->__toString();
125
            }
126
127
            if (is_array($item)) {
128
                $data[$key] = $this->prepareUnserializableData($item);
129
            }
130
        }
131
132
        return $data;
133
    }
134
}
135