PsrHandlerFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 16
c 1
b 0
f 0
dl 0
loc 35
ccs 8
cts 8
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 26 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasMonolog\Factory\Handler;
6
7
use Arp\LaminasFactory\AbstractFactory;
8
use Arp\LaminasMonolog\Factory\FactoryFormatterProviderTrait;
9
use Arp\LaminasMonolog\Factory\FactoryLoggerProviderTrait;
10
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
11
use Monolog\Handler\PsrHandler;
12
use Monolog\Logger;
13
use Psr\Container\ContainerExceptionInterface;
14
use Psr\Container\ContainerInterface;
15
use Psr\Log\LogLevel;
16
17
/**
18
 * @phpstan-import-type Level from \Monolog\Logger
19
 * @phpstan-import-type LevelName from \Monolog\Logger
20
 */
21
final class PsrHandlerFactory extends AbstractFactory
22
{
23
    use FactoryLoggerProviderTrait;
24
    use FactoryFormatterProviderTrait;
25
26
    /**
27
     * @throws ServiceNotCreatedException
28
     * @throws ContainerExceptionInterface
29
     */
30
    public function __invoke(ContainerInterface $container, string $requestedName, array $options = null): PsrHandler
31
    {
32
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
33
34
        if (empty($options['logger'])) {
35
            throw new ServiceNotCreatedException(
36
                sprintf('The required \'logger\' configuration option is missing for service \'%s\'', $requestedName)
37
            );
38
        }
39
40 4
        /** @var Level|LevelName|LogLevel::* $level */
41
        $level = isset($options['level']) ? (int)$options['level'] : Logger::DEBUG;
42 4
43
        $handler = new PsrHandler(
44 4
            $this->getLogger($container, $options['logger'], $requestedName),
45 1
            $level,
46 1
            !isset($options['bubble']) || $options['bubble']
47
        );
48
49
        if (!empty($options['formatter'])) {
50
            $handler->setFormatter(
51 3
                $this->getFormatter($container, $options['formatter'], $requestedName)
52
            );
53 3
        }
54 3
55
        return $handler;
56 3
    }
57
}
58