fractalzombie /
frzb-request-mapper
| 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
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 |