Completed
Push — master ( 22860b...81e1d8 )
by Alessandro
02:32
created

MongoDbDataCollector::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace Facile\MongoDbBundle\DataCollector;
4
5
use Facile\MongoDbBundle\Models\Query;
6
use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
10
11
/**
12
 * Class MongoDbDataCollector.
13
 * @internal
14
 */
15
final class MongoDbDataCollector extends DataCollector
16
{
17
    const QUERY_KEYWORD = 'queries';
18
    const CONNECTION_KEYWORD = 'connections';
19
    const TIME_KEYWORD = 'totalTime';
20
21
    /** @var DataCollectorLoggerInterface */
22
    private $logger;
23
    /** @var array */
24
    protected $data;
25
26 1
    public function __construct()
27
    {
28 1
        $this->data = [
29 1
            self::QUERY_KEYWORD => [],
30 1
            self::TIME_KEYWORD => 0.0,
31 1
            self::CONNECTION_KEYWORD => [],
32
        ];
33 1
    }
34
35
    /**
36
     * @param DataCollectorLoggerInterface $logger
37
     */
38 1
    public function setLogger(DataCollectorLoggerInterface $logger)
39
    {
40 1
        $this->logger = $logger;
41 1
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    public function collect(Request $request, Response $response, \Exception $exception = null)
47
    {
48 1
        while ($this->logger->hasLoggedEvents()) {
49
            /** @var Query $event */
50 1
            $event = $this->logger->getLoggedEvent();
51
52
            // with extension version under 1.2.4 some Mongo objects can't be automatically serialized
53 1
            if (-1 === version_compare(phpversion('mongodb'), '1.2.4')) {
54 1
                MongoQuerySerializer::serialize($event);
55
            }
56
57 1
            $this->data[self::QUERY_KEYWORD][] = $event;
58 1
            $this->data[self::TIME_KEYWORD] += $event->getExecutionTime();
59
        }
60
61 1
        $this->data[self::CONNECTION_KEYWORD] = $this->logger->getConnections();
62 1
    }
63
64
    /**
65
     * @return int
66
     */
67 1
    public function getQueryCount(): int
68
    {
69 1
        return count($this->data[self::QUERY_KEYWORD]);
70
    }
71
72
    /**
73
     * @return array
74
     */
75 1
    public function getQueries(): array
76
    {
77 1
        return $this->data[self::QUERY_KEYWORD];
78
    }
79
80
    /**
81
     * @return float
82
     */
83 1
    public function getTime(): float
84
    {
85 1
        return (float) ($this->data[self::TIME_KEYWORD] * 1000);
86
    }
87
88
    /**
89
     * @return int
90
     */
91 1
    public function getConnectionsCount(): int
92
    {
93 1
        return count($this->data[self::CONNECTION_KEYWORD]);
94
    }
95
    /**
96
     * @return array|string[]
97
     */
98 1
    public function getConnections(): array
99
    {
100 1
        return $this->data[self::CONNECTION_KEYWORD];
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1
    public function getName()
107
    {
108 1
        return 'mongodb';
109
    }
110
}
111