ExceptionFormatterLocator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 5 1
A __construct() 0 5 1
A has() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\ExceptionFormatter;
6
7
use Fp\Collections\HashMap;
8
use FRZB\Component\DependencyInjection\Attribute\AsService;
9
use FRZB\Component\RequestMapper\ExceptionFormatter\Formatter\FormatterInterface as Formatter;
10
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
11
12
#[AsService]
13
class ExceptionFormatterLocator implements ExceptionFormatterLocatorInterface
14
{
15
    /** @var HashMap<string, callable|Formatter> */
16
    private readonly HashMap $formatters;
17
18 6
    public function __construct(
19
        #[TaggedIterator(Formatter::class, defaultIndexMethod: 'getExceptionClass', defaultPriorityMethod: 'getPriority')]
20
        iterable $formatters,
21
    ) {
22 6
        $this->formatters = HashMap::collect($formatters);
0 ignored issues
show
Bug introduced by
The property formatters is declared read-only in FRZB\Component\RequestMa...ceptionFormatterLocator.
Loading history...
23
    }
24
25 6
    public function get(\Throwable $e): Formatter|callable
26
    {
27 6
        return $this->formatters
28 6
            ->get($e::class)
29 6
            ->getOrElse($this->formatters->get(\Throwable::class)->get())
30
        ;
31
    }
32
33 3
    public function has(\Throwable $e): bool
34
    {
35 3
        return $this->formatters->get($e::class)->isNonEmpty();
36
    }
37
}
38