Completed
Branch query_logging_to_events (025608)
by Alessandro
02:17
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
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
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\Models\QueryLog;
8
9
/**
10
 * Class MongoLogger
11
 */
12
class MongoLogger implements DataCollectorLoggerInterface
13
{
14
    /** @var \SplQueue|QueryLog[] */
15
    private $logs;
16
17
    /** @var array|string[] */
18
    private $connections;
19
20
    /**
21
     * MongoLogger constructor.
22
     */
23 5
    public function __construct()
24
    {
25 5
        $this->logs = new \SplQueue();
26 5
        $this->connections = [];
27 5
    }
28
29
    /**
30
     * @param string $connection
31
     */
32 4
    public function addConnection(string $connection)
33
    {
34 4
        $this->connections[] = $connection;
35 4
    }
36
37
    /**
38
     * @return array|\string[]
39
     */
40 2
    public function getConnections(): array
41
    {
42 2
        return $this->connections;
43
    }
44
45
    /**
46
     * @param QueryLog $event
47
     */
48 2
    public function logQuery(QueryLog $event)
49
    {
50 2
        $this->logs->enqueue($event);
51 2
    }
52
53
    /**
54
     * @return bool
55
     */
56 2
    public function hasLoggedEvents(): bool
57
    {
58 2
        return !$this->logs->isEmpty();
59
    }
60
61
    /**
62
     * @return QueryLog
63
     */
64 2
    public function getLoggedEvent(): QueryLog
65
    {
66 2
        if (!$this->hasLoggedEvents()) {
67 1
            throw new \LogicException('No more events logged!');
68
        }
69
70 2
        return $this->logs->dequeue();
71
    }
72
}
73