Passed
Pull Request — master (#1)
by Alex
08:35
created

PsrHandlerFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 16
c 1
b 0
f 0
dl 0
loc 40
ccs 0
cts 13
cp 0
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 Laminas\ServiceManager\ServiceLocatorInterface;
12
use Monolog\Handler\PsrHandler;
13
use Monolog\Logger;
14
use Psr\Container\ContainerInterface;
15
use Psr\Log\LogLevel;
16
17
/**
18
 * @author Alex Patterson <[email protected]>
19
 * @package Arp\LaminasMonolog\Factory\Handler
20
 *
21
 * @phpstan-import-type Level from \Monolog\Logger
22
 * @phpstan-import-type LevelName from \Monolog\Logger
23
 */
24
final class PsrHandlerFactory extends AbstractFactory
25
{
26
    use FactoryLoggerProviderTrait;
27
    use FactoryFormatterProviderTrait;
28
29
    /**
30
     * @param ContainerInterface&ServiceLocatorInterface $container
31
     * @param string                                     $requestedName
32
     * @param array<mixed>|null                          $options
33
     *
34
     * @return PsrHandler
35
     *
36
     * @throws ServiceNotCreatedException
37
     */
38
    public function __invoke(ContainerInterface $container, string $requestedName, array $options = null): PsrHandler
39
    {
40
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
41
42
        if (empty($options['logger'])) {
43
            throw new ServiceNotCreatedException(
44
                sprintf('The required \'logger\' configuration option is missing for service \'%s\'', $requestedName)
45
            );
46
        }
47
48
        /** @var Level|LevelName|LogLevel::* $level */
49
        $level = isset($options['level']) ? (int)$options['level'] : Logger::DEBUG;
50
51
        $handler = new PsrHandler(
52
            $this->getLogger($container, $options['logger'], $requestedName),
53
            $level,
54
            !isset($options['bubble']) || $options['bubble']
55
        );
56
57
        if (!empty($options['formatter'])) {
58
            $handler->setFormatter(
59
                $this->getFormatter($container, $options['formatter'], $requestedName)
60
            );
61
        }
62
63
        return $handler;
64
    }
65
}
66