Completed
Pull Request — master (#8)
by Alessandro
02:58
created

MongoLogger::hasLoggedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
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
    /**
22
     * MongoLogger constructor.
23
     */
24 6
    public function __construct()
25
    {
26 6
        $this->logs = new \SplQueue();
27 6
        $this->connections = [];
28 6
    }
29
30 1
    public function startLogging(LogEvent $event)
31
    {
32 1
        $event->setStart(microtime(true));
33 1
    }
34
35
    /**
36
     * @param string $connection
37
     */
38 4
    public function addConnection(string $connection)
39
    {
40 4
        $this->connections[] = $connection;
41 4
    }
42
43
    /**
44
     * @return array|\string[]
45
     */
46 2
    public function getConnections(): array
47
    {
48 2
        return $this->connections;
49
    }
50
51
    /**
52
     * @param LogEvent $event
53
     */
54 2
    public function logQuery(LogEvent $event)
55
    {
56 2
        $executionTime = microtime(true) - $event->getStart();
57 2
        $event->setExecutionTime($executionTime);
58
59 2
        $this->logs->enqueue($event);
60 2
    }
61
62
    /**
63
     * @return bool
64
     */
65 2
    public function hasLoggedEvents(): bool
66
    {
67 2
        return !$this->logs->isEmpty();
68
    }
69
70
    /**
71
     * @return LogEvent
72
     */
73 2
    public function getLoggedEvent(): LogEvent
74
    {
75 2
        if (!$this->hasLoggedEvents()) {
76 1
            throw new \LogicException('No more events logged!');
77
        }
78
79 2
        return $this->logs->dequeue();
80
    }
81
}
82