Passed
Pull Request — master (#1)
by Alex
02:54
created

PsrHandlerFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

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