ExceptionFormatterLocator::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
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