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

Logger::warning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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