PsrHandlerFactory::__invoke()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 26
ccs 8
cts 8
cp 1
rs 9.5222
cc 5
nc 5
nop 3
crap 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