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

MongoLogger   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A startLogging() 0 4 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
    /**
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