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

ExceptionFormatterLocator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 21
ccs 8
cts 8
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A get() 0 3 1
A has() 0 3 1
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