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

Handlers   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 8
dl 0
loc 31
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

4 Methods

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