Completed
Pull Request — master (#8)
by Alessandro
06:57
created

MongoLogger   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 2
cbo 3
dl 0
loc 77
ccs 24
cts 24
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A startLogging() 0 6 1
A addConnection() 0 4 1
A getConnections() 0 4 1
A logQuery() 0 7 1
A hasLoggedEvents() 0 4 1
A getLoggedEvent() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\MongoDbBundle\Services\Loggers;
6
7
use Facile\MongoDbBundle\Services\Loggers\Model\LogEvent;
8
use Symfony\Component\Stopwatch\Stopwatch;
9
10
/**
11
 * Class MongoLogger
12
 */
13
class MongoLogger implements DataCollectorLoggerInterface
14
{
15
    /** @var \SplQueue|LogEvent[] */
16
    private $logs;
17
18
    /** @var array|string[] */
19
    private $connections;
20
21
    /** @var Stopwatch */
22
    private $stopwatch;
23
24
    /**
25
     * MongoLogger constructor.
26
     *
27
     * @param Stopwatch $stopwatch
28
     */
29 6
    public function __construct(Stopwatch $stopwatch)
30
    {
31 6
        $this->logs = new \SplQueue();
32 6
        $this->connections = [];
33 6
        $this->stopwatch = $stopwatch;
34 6
    }
35
36 1
    public function startLogging(LogEvent $event)
37
    {
38 1
        $watchKey = uniqid('mongodb_query_log_');
39 1
        $event->setWatchKey($watchKey);
40
        $this->stopwatch->start($watchKey);
41
    }
42
43
    /**
44 4
     * @param string $connection
45
     */
46 4
    public function addConnection(string $connection)
47 4
    {
48
        $this->connections[] = $connection;
49
    }
50
51
    /**
52 2
     * @return array|\string[]
53
     */
54 2
    public function getConnections(): array
55
    {
56
        return $this->connections;
57
    }
58
59
    /**
60 2
     * @param LogEvent $event
61
     */
62 2
    public function logQuery(LogEvent $event)
63 2
    {
64
        $watch = $this->stopwatch->stop($event->getWatchKey());
65 2
        $event->setExecutionTime($watch->getDuration());
66 2
67
        $this->logs->enqueue($event);
68
    }
69
70
    /**
71 2
     * @return bool
72
     */
73 2
    public function hasLoggedEvents(): bool
74
    {
75
        return !$this->logs->isEmpty();
76
    }
77
78
    /**
79 2
     * @return LogEvent
80
     */
81 2
    public function getLoggedEvent(): LogEvent
82 1
    {
83
        if (!$this->hasLoggedEvents()) {
84
            throw new \LogicException('No more events logged!');
85 2
        }
86
87
        return $this->logs->dequeue();
88
    }
89
}
90