Passed
Push — main ( 55fc6d...13bcd8 )
by Chema
52s queued 13s
created

Handlers::addBuiltInHandlers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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