Issues (48)

Logger/ContextExtractorLocator.php (1 issue)

Labels
Severity
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 FRZB\Component\DependencyInjection\Attribute\AsService;
19
use FRZB\Component\MetricsPower\Logger\ContextExtractor\ContextExtractorInterface;
20
use FRZB\Component\MetricsPower\Logger\ContextExtractor\DefaultContextExtractor;
21
use FRZB\Component\MetricsPower\Logger\Exception\ContextExtractorLocatorException;
22
use Psr\Container\ContainerExceptionInterface;
23
use Psr\Container\ContainerInterface;
24
use Psr\Container\NotFoundExceptionInterface;
25
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
26
27
#[AsService]
28
final readonly class ContextExtractorLocator implements ContextExtractorLocatorInterface
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 28 at column 6
Loading history...
29
{
30
    public function __construct(
31
        #[TaggedLocator(ContextExtractorInterface::class, defaultIndexMethod: 'getType')]
32
        private ContainerInterface $serviceProvider,
33
    ) {}
34
35
    public function get(object|string $target): ContextExtractorInterface
36
    {
37
        try {
38
            return $this->serviceProvider->get(\is_object($target) ? $target::class : $target);
39
        } catch (NotFoundExceptionInterface) {
40
            return $this->serviceProvider->get(DefaultContextExtractor::getType());
41
        } catch (ContainerExceptionInterface $e) {
42
            throw ContextExtractorLocatorException::fromThrowable($e);
43
        }
44
    }
45
}
46