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

Logger::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
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