Completed
Push — master ( 3fa8fc...dd5833 )
by Alessandro
13s
created

MongoDbDataCollector   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 94
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setLogger() 0 4 1
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
A collect() 0 17 3
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
24 1
    public function __construct()
25
    {
26 1
        $this->data = [
27 1
            self::QUERY_KEYWORD => [],
28 1
            self::TIME_KEYWORD => 0.0,
29 1
            self::CONNECTION_KEYWORD => [],
30
        ];
31 1
    }
32
33
    /**
34
     * @param DataCollectorLoggerInterface $logger
35
     */
36 1
    public function setLogger(DataCollectorLoggerInterface $logger)
37
    {
38 1
        $this->logger = $logger;
39 1
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    public function collect(Request $request, Response $response, \Exception $exception = null)
45
    {
46 1
        while ($this->logger->hasLoggedEvents()) {
47
            /** @var Query $event */
48 1
            $event = $this->logger->getLoggedEvent();
49
50
            // with extension version under 1.2.4 some Mongo objects can't be automatically serialized
51 1
            if (-1 === version_compare(phpversion('mongodb'), '1.2.4')) {
52 1
                MongoQuerySerializer::serialize($event);
53
            }
54
55 1
            $this->data[self::QUERY_KEYWORD][] = $event;
56 1
            $this->data[self::TIME_KEYWORD] += $event->getExecutionTime();
57
        }
58
59 1
        $this->data[self::CONNECTION_KEYWORD] = $this->logger->getConnections();
60 1
    }
61
62
    /**
63
     * @return int
64
     */
65 1
    public function getQueryCount(): int
66
    {
67 1
        return count($this->data[self::QUERY_KEYWORD]);
68
    }
69
70
    /**
71
     * @return array
72
     */
73 1
    public function getQueries(): array
74
    {
75 1
        return $this->data[self::QUERY_KEYWORD];
76
    }
77
78
    /**
79
     * @return float
80
     */
81 1
    public function getTime(): float
82
    {
83 1
        return (float)($this->data[self::TIME_KEYWORD] * 1000);
84
    }
85
86
    /**
87
     * @return int
88
     */
89 1
    public function getConnectionsCount(): int
90
    {
91 1
        return count($this->data[self::CONNECTION_KEYWORD]);
92
    }
93
    /**
94
     * @return array|string[]
95
     */
96 1
    public function getConnections(): array
97
    {
98 1
        return $this->data[self::CONNECTION_KEYWORD];
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 1
    public function getName()
105
    {
106 1
        return 'mongodb';
107
    }
108
}
109