Passed
Push — master ( 5f3d2e...2722b4 )
by Konrad
21:26 queued 17:04
created

Logger   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 48
ccs 20
cts 21
cp 0.9524
rs 10
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 error() 0 3 1
A __construct() 0 5 1
1
<?php
2
3
namespace sweetrdf\InMemoryStoreSqlite\Log;
4
5
use Exception;
6
7
class Logger
8
{
9
    /**
10
     * @var array<string,array<integer,mixed>>
11
     */
12
    private array $entries = [];
13
14 108
    public function __construct()
15
    {
16 108
        $this->entries = [
17 108
            'error' => [],
18 108
            'warning' => [],
19 108
        ];
20
    }
21
22 11
    public function error(string $message, array $context = []): void
23
    {
24 11
        $this->log('error', $message, $context);
25
    }
26
27 5
    public function warning(string $message, array $context = []): void
28
    {
29 5
        $this->log('warning', $message, $context);
30
    }
31
32 11
    private function log($level, $message, array $context = []): void
33
    {
34 11
        if (!isset($this->entries[$level])) {
35
            $this->entries[$level] = [];
36
        }
37
38 11
        $this->entries[$level][] = ['message' => $message, 'context' => $context];
39
    }
40
41 104
    public function getEntries(?string $level = null): array
42
    {
43 104
        if (null !== $level && isset($this->entries[$level])) {
44 100
            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 10
    public function hasEntries(?string $level = null): bool
53
    {
54 10
        return 0 < \count($this->getEntries($level));
55
    }
56
}
57