Passed
Branch extract-store (f24e42)
by Konrad
04:37
created

Logger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace sweetrdf\InMemoryStoreSqlite;
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 117
    public function __construct()
15
    {
16 117
        $this->entries = [
17
            'error' => [],
18
            'warning' => [],
19
        ];
20 117
    }
21
22 3
    public function error(string $message, array $context = []): void
23
    {
24 3
        $this->log('error', $message, $context);
25 3
    }
26
27 3
    public function warning(string $message, array $context = []): void
28
    {
29 3
        $this->log('warning', $message, $context);
30 3
    }
31
32 3
    private function log($level, $message, array $context = []): void
33
    {
34 3
        if (!isset($this->entries[$level])) {
35
            $this->entries[$level] = [];
36
        }
37
38 3
        $this->entries[$level][] = ['message' => $message, 'context' => $context];
39 3
    }
40
41 5
    public function getEntries(?string $level = null): array
42
    {
43 5
        if (null !== $level && isset($this->entries[$level])) {
44 2
            return $this->entries[$level];
45 5
        } 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 3
            return $this->entries;
47
        }
48
49 2
        throw new Exception('Level '.$level.' not set.');
50
    }
51
52 2
    public function hasEntries(?string $level = null): bool
53
    {
54 2
        return 0 < \count($this->getEntries($level));
55
    }
56
}
57