Completed
Pull Request — master (#8)
by Alessandro
03:13
created

MongoLogger::startLogging()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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