Passed
Push — main ( e0948b...5456f8 )
by Fractal
02:50
created

ContextExtractorLocator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 17
rs 10
c 1
b 0
f 0
wmc 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
7
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
9
 *
10
 * Copyright (c) 2024 Mykhailo Shtanko [email protected]
11
 *
12
 * For the full copyright and license information, please view the LICENSE.MD
13
 * file that was distributed with this source code.
14
 */
15
16
namespace FRZB\Component\MetricsPower\Logger;
17
18
use Fp\Collections\HashMap;
19
use FRZB\Component\DependencyInjection\Attribute\AsService;
20
use FRZB\Component\MetricsPower\Logger\ContextExtractor\ContextExtractorInterface;
21
use FRZB\Component\MetricsPower\Logger\ContextExtractor\DefaultContextExtractor;
22
use FRZB\Component\MetricsPower\Logger\Exception\ContextExtractorLocatorException;
23
use Psr\Container\ContainerExceptionInterface;
24
use Psr\Container\NotFoundExceptionInterface;
25
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
26
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
27
use Symfony\Component\DependencyInjection\ServiceLocator;
28
use Symfony\Contracts\Service\ServiceProviderInterface;
29
30
#[AsService]
31
final readonly class ContextExtractorLocator implements ContextExtractorLocatorInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 31 at column 6
Loading history...
32
{
33
    public function __construct(
34
        #[TaggedLocator(ContextExtractorInterface::class, defaultIndexMethod: 'getType')]
35
        private ServiceProviderInterface $serviceProvider,
36
    ) {
37
    }
38
39
    public function get(object|string $target): ContextExtractorInterface
40
    {
41
        try {
42
            return $this->serviceProvider->get(\is_object($target) ? $target::class : $target);
43
        } catch (NotFoundExceptionInterface) {
44
            return $this->serviceProvider->get(DefaultContextExtractor::getType());
45
        } catch (ContainerExceptionInterface $e) {
46
            throw ContextExtractorLocatorException::fromThrowable($e);
47
        }
48
    }
49
}
50