Completed
Push — master ( b0b219...3b5b93 )
by Márk
140:06 queued 76:01
created

TestErrorHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Nofw\Error;
4
5
/**
6
 * This handler can be used in tests.
7
 *
8
 * @author Márk Sági-Kazár <[email protected]>
9
 */
10
final class TestErrorHandler implements ErrorHandler
11
{
12
    /**
13
     * @var \SplObjectStorage
14
     */
15
    private $errors;
16
17
    public function __construct()
18
    {
19
        $this->errors = new \SplObjectStorage();
20
    }
21
22
    public function handle(\Throwable $t, array $context = []): void
23
    {
24
        $this->errors->attach($t, $context);
25
    }
26
27
    /**
28
     * Checks whether an error has been handled.
29
     */
30
    public function contains(\Throwable $t): bool
31
    {
32
        return $this->errors->contains($t);
33
    }
34
35
    /**
36
     * Returns the context for an error or null if not found.
37
     */
38
    public function getContext(\Throwable $t): ?array
39
    {
40
        if ($this->errors->contains($t)) {
41
            return $this->errors[$t];
42
        }
43
44
        return null;
45
    }
46
47
    /**
48
     * Returns the errors in the form of [error, context].
49
     */
50
    public function getErrors(): array
51
    {
52
        $errors = [];
53
54
        foreach ($this->errors as $error) {
55
            $errors[] = [$error, $this->errors[$error]];
56
        }
57
58
        return $errors;
59
    }
60
}
61