Passed
Push — main ( 94bc2d...615377 )
by Chema
03:24 queued 56s
created

Handlers::addBuiltInHandlers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router\Configure;
6
7
use Exception;
8
use Gacela\Router\Exceptions\NotFound404Exception;
9
use Gacela\Router\Handlers\FallbackExceptionHandler;
10
use Gacela\Router\Handlers\NotFound404ExceptionHandler;
11
12
final class Handlers
13
{
14
    /** @var array<class-string, callable|class-string> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, callable|class-string> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, callable|class-string>.
Loading history...
15
    private array $handlers = [];
16
17 97
    public function __construct()
18
    {
19 97
        $this->addBuiltInHandlers();
20
    }
21
    /**
22
     * @param class-string<Exception> $exception
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Exception> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Exception>.
Loading history...
23
     * @param callable|class-string $handler
24
     */
25 97
    public function handle(string $exception, callable|string $handler): self
26
    {
27 97
        $this->handlers[$exception] = $handler;
28 97
        return $this;
29
    }
30
31
    /**
32
     * @return array<class-string, callable|class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, callable|class-string> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, callable|class-string>.
Loading history...
33
     */
34 18
    public function getAllHandlers(): array
35
    {
36 18
        return $this->handlers;
37
    }
38
39 97
    private function addBuiltInHandlers(): void
40
    {
41 97
        $this->handle(NotFound404Exception::class, NotFound404ExceptionHandler::class);
42 97
        $this->handle(Exception::class, FallbackExceptionHandler::class);
43
    }
44
}
45