Passed
Push — extract-store ( ec1a3e...70fc7f )
by Konrad
04:10
created

Logger   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 48
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 7 2
A getEntries() 0 9 4
A hasEntries() 0 3 1
A warning() 0 3 1
A __construct() 0 5 1
A error() 0 3 1
1
<?php
2
3
namespace sweetrdf\InMemoryStoreSqlite\Log;
4
5
use Exception;
6
7
final class Logger
8
{
9
    /**
10
     * @var array<string,array<integer,mixed>>
11
     */
12
    private array $entries = [];
13
14 111
    public function __construct()
15
    {
16 111
        $this->entries = [
17
            'error' => [],
18
            'warning' => [],
19
        ];
20 111
    }
21
22 24
    public function error(string $message, array $context = []): void
23
    {
24 24
        $this->log('error', $message, $context);
25 24
    }
26
27 5
    public function warning(string $message, array $context = []): void
28
    {
29 5
        $this->log('warning', $message, $context);
30 5
    }
31
32 24
    private function log($level, $message, array $context = []): void
33
    {
34 24
        if (!isset($this->entries[$level])) {
35
            $this->entries[$level] = [];
36
        }
37
38 24
        $this->entries[$level][] = ['message' => $message, 'context' => $context];
39 24
    }
40
41 108
    public function getEntries(?string $level = null): array
42
    {
43 108
        if (null !== $level && isset($this->entries[$level])) {
44 104
            return $this->entries[$level];
45 9
        } elseif (null == $level) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $level of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
46 5
            return $this->entries;
47
        }
48
49 4
        throw new Exception('Level '.$level.' not set.');
50
    }
51
52 32
    public function hasEntries(?string $level = null): bool
53
    {
54 32
        return 0 < \count($this->getEntries($level));
55
    }
56
}
57