Completed
Pull Request — master (#8)
by Alessandro
06:59 queued 04:03
created

MongoLogger::logQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
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()
37
    {
38 1
        $this->stopwatch->start('mongodb_query_log');
39 1
    }
40
41
    /**
42
     * @param string $connection
43
     */
44 4
    public function addConnection(string $connection)
45
    {
46 4
        $this->connections[] = $connection;
47 4
    }
48
49
    /**
50
     * @return array|\string[]
51
     */
52 2
    public function getConnections(): array
53
    {
54 2
        return $this->connections;
55
    }
56
57
    /**
58
     * @param LogEvent $event
59
     */
60 2
    public function logQuery(LogEvent $event)
61
    {
62 2
        $watch = $this->stopwatch->stop('mongodb_query_log');
63 2
        $event->setExecutionTime($watch->getDuration());
64
65 2
        $this->logs->enqueue($event);
66 2
    }
67
68
    /**
69
     * @return bool
70
     */
71 2
    public function hasLoggedEvents(): bool
72
    {
73 2
        return !$this->logs->isEmpty();
74
    }
75
76
    /**
77
     * @return LogEvent
78
     */
79 2
    public function getLoggedEvent(): LogEvent
80
    {
81 2
        if (!$this->hasLoggedEvents()) {
82 1
            throw new \LogicException('No more events logged!');
83
        }
84
85 2
        return $this->logs->dequeue();
86
    }
87
}
88