Completed
Push — master ( 5c0846...25f19b )
by Alessandro
06:16
created

MongoDbDataCollector::prepareUnserializableData()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.1782

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
ccs 11
cts 13
cp 0.8462
rs 6.7272
cc 7
eloc 13
nc 9
nop 1
crap 7.1782
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Facile\MongoDbBundle\DataCollector;
6
7
use Facile\MongoDbBundle\Models\LogEvent;
8
use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface;
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 2
            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 (-1 === version_compare(phpversion('mongodb'), '1.2.0')) {
53 1
                $event->setData($this->prepareUnserializableData($event->getData()));
54 1
                $event->setFilters($this->prepareUnserializableData($event->getFilters()));
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|object $data
113
     *
114
     * @return array|object
115
     */
116 1
    private function prepareUnserializableData($data)
117
    {
118 1
        foreach ($data as $key => $item) {
119 1
            if (method_exists($item, 'getArrayCopy')) {
120 1
                $data[$key] = $this->prepareUnserializableData($item->getArrayCopy());
121
            }
122
123 1
            if (method_exists($item, 'toDateTime')) {
124 1
                $data[$key] = $item->toDateTime()->format('r');
125 1
                continue;
126
            }
127
128 1
            if (method_exists($item, '__toString')) {
129
                $data[$key] = $item->__toString();
130
                continue;
131
            }
132
133 1
            if (is_array($item) || is_object($item)) {
134 1
                $data[$key] = $this->prepareUnserializableData((array)$item);
135
            }
136
        }
137
138 1
        return $data;
139
    }
140
}
141