StreamHandlerFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 25
c 1
b 0
f 0
dl 0
loc 45
ccs 0
cts 16
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 37 3
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 Laminas\ServiceManager\Exception\ServiceNotCreatedException;
10
use Monolog\Formatter\LineFormatter;
11
use Monolog\Handler\StreamHandler;
12
use Monolog\Logger;
13
use Psr\Container\ContainerExceptionInterface;
14
use Psr\Container\ContainerInterface;
15
16
final class StreamHandlerFactory extends AbstractFactory
17
{
18
    use FactoryFormatterProviderTrait;
19
20
    /**
21
     * @throws ServiceNotCreatedException
22
     * @throws ContainerExceptionInterface
23
     */
24
    public function __invoke(ContainerInterface $container, string $requestedName, array $options = null): StreamHandler
25
    {
26
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
27
28
        $stream = $options['stream'] ?? null;
29
        if (empty($stream)) {
30
            throw new ServiceNotCreatedException(
31
                sprintf('The required \'stream\' configuration option is missing for service \'%s\'', $requestedName)
32
            );
33
        }
34
35
        $className = $options['class_name'] ?? StreamHandler::class;
36
        if (!is_a($className, StreamHandler::class, true)) {
37
            throw new ServiceNotCreatedException(
38
                sprintf(
39
                    'The stream handler provided via configuration option \'class_name\' is invalid: '
40
                    . 'The stream handler class must extend from \'%s\'; \'%s\' provided for service \'%s\'',
41
                    StreamHandler::class,
42
                    $className,
43
                    $requestedName
44
                )
45
            );
46
        }
47
48
        $streamHandler = new $className(
49
            $stream,
50
            $options['level'] ?? Logger::DEBUG,
51
            $options['bubble'] ?? true,
52
            $options['file_permission'] ?? null,
53
            $options['use_locking'] ?? true
54
        );
55
56
        $streamHandler->setFormatter(
57
            $this->getFormatter($container, $options['formatter'] ?? LineFormatter::class, $requestedName),
58
        );
59
60
        return $streamHandler;
61
    }
62
}
63