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

PsrHandlerFactory::__invoke()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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