Passed
Push — master ( a8d0c9...8a0571 )
by Aleksei
07:15 queued 22s
created

LoggerInjector::createInjection()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 7
c 2
b 0
f 1
dl 0
loc 21
ccs 8
cts 8
cp 1
rs 10
cc 4
nc 6
nop 2
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Logger;
6
7
use Psr\Log\LoggerInterface;
8
use Spiral\Core\Container\InjectorInterface;
9
use Spiral\Logger\Attribute\LoggerChannel;
10
11
/**
12
 * Container injector for {@see LoggerInterface}.
13
 * Supports {@see LoggerChannel} attribute.
14
 *
15
 * @implements InjectorInterface<LoggerInterface>
16
 */
17
final class LoggerInjector implements InjectorInterface
18
{
19
    public const DEFAULT_CHANNEL = 'default';
20
21 7
    public function __construct(
22
        private readonly LogsInterface $factory,
23
    ) {
24 7
    }
25
26
    /**
27
     * @param \ReflectionParameter|string|null $context may use extended context if possible.
28
     */
29 7
    public function createInjection(
30
        \ReflectionClass $class,
31
        \ReflectionParameter|null|string $context = null,
32
    ): LoggerInterface {
33 7
        $channel = \is_object($context) ? $this->extractChannelAttribute($context) : null;
34
35 7
        if ($channel === null) {
36
            /**
37
             * Array of flags to check if the logger allows null argument
38
             *
39
             * @var array<class-string<LogsInterface>, bool> $cache
40
             */
41 5
            static $cache = [];
42
43 5
            $cache[$this->factory::class] = (new \ReflectionMethod($this->factory, 'getLogger'))
44 5
                ->getParameters()[0]->allowsNull();
45
46 5
            $channel = $cache[$this->factory::class] ? null : self::DEFAULT_CHANNEL;
47
        }
48
49 7
        return $this->factory->getLogger($channel);
0 ignored issues
show
Bug introduced by
It seems like $channel can also be of type null; however, parameter $channel of Spiral\Logger\LogsInterface::getLogger() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        return $this->factory->getLogger(/** @scrutinizer ignore-type */ $channel);
Loading history...
50
    }
51
52
    /**
53
     * @return non-empty-string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
54
     */
55 2
    private function extractChannelAttribute(\ReflectionParameter $parameter): ?string
56
    {
57
        /** @var \ReflectionAttribute<LoggerChannel>[] $attributes */
58 2
        $attributes = $parameter->getAttributes(LoggerChannel::class);
59
60 2
        return $attributes[0]?->newInstance()->name;
61
    }
62
}
63