Passed
Push — main ( df85bc...337254 )
by Fractal
02:51
created

ExceptionFormatterLocator::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Locator;
6
7
use FRZB\Component\DependencyInjection\Attribute\AsService;
8
use FRZB\Component\RequestMapper\ExceptionFormatter\Formatter\FormatterInterface;
9
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
10
11
#[AsService]
12 1
class ExceptionFormatterLocator implements ExceptionFormatterLocatorInterface
13
{
14
    /** @var array<FormatterInterface> */
15
    private array $formatters;
16
17 6
    public function __construct(
18
        #[TaggedIterator(self::EXCEPTION_FORMATTERS_TAG, defaultIndexMethod: 'getExceptionClass', defaultPriorityMethod: 'getPriority')]
19
        iterable $formatters
20
    ) {
21 6
        $this->formatters = $formatters instanceof \Traversable ? iterator_to_array($formatters) : (array) $formatters;
22 6
    }
23
24 6
    public function get(\Throwable $e): FormatterInterface
25
    {
26 6
        return $this->formatters[$e::class] ?? $this->formatters[\Throwable::class];
27
    }
28
29 3
    public function has(\Throwable $e): bool
30
    {
31 3
        return \array_key_exists($e::class, $this->formatters);
32
    }
33
}
34