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

Handlers   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 35
ccs 15
cts 15
cp 1
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getAllHandlers() 0 3 1
A handle() 0 4 1
A addBuiltInHandlers() 0 9 1
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