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

FactoryLoggerProviderTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 35.29%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 20
c 2
b 0
f 0
dl 0
loc 58
ccs 6
cts 17
cp 0.3529
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getLogger() 0 32 8
A setLoggerService() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasMonolog\Factory;
6
7
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
8
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
9
use Laminas\ServiceManager\ServiceLocatorInterface;
10
use Monolog\Logger;
11
use Psr\Container\ContainerExceptionInterface;
12
use Psr\Container\ContainerInterface;
13
use Psr\Container\NotFoundExceptionInterface;
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\NullLogger;
16
17
/**
18
 * Provides service factories the ability to resolve loggers from the container
19
 *
20
 * @author  Alex Patterson <[email protected]>
21
 * @package Arp\LaminasMonolog\Factory
22
 */
23
trait FactoryLoggerProviderTrait
24
{
25
    /**
26
     * @var string
27
     */
28
    protected string $loggerService = Logger::class;
29
30
    /**
31
     * @param ContainerInterface|ServiceLocatorInterface $container
32
     * @param LoggerInterface|string|array<mixed>        $logger
33
     * @param string                                     $serviceName
34
     *
35
     * @return LoggerInterface
36
     *
37
     * @throws ServiceNotCreatedException
38
     * @throws ContainerExceptionInterface
39
     * @throws NotFoundExceptionInterface
40
     */
41 3
    public function getLogger(ContainerInterface $container, $logger, string $serviceName): LoggerInterface
42
    {
43 3
        if (is_string($logger)) {
44
            if (!$container->has($logger)) {
45
                throw new ServiceNotFoundException(
46
                    sprintf('The logger \'%s\' could not be found for service \'%s\'', $logger, $serviceName)
47
                );
48
            }
49
50
            $logger = $container->get($logger);
51
        }
52
53 3
        if (is_array($logger) && $container instanceof ServiceLocatorInterface) {
54
            $logger = $container->build($this->loggerService, $logger);
55
        }
56
57 3
        if (null === $logger) {
58
            $logger = new NullLogger();
59
        }
60
61 3
        if (!$logger instanceof LoggerInterface) {
62
            throw new ServiceNotCreatedException(
63
                sprintf(
64
                    'The resolved logger must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
65
                    LoggerInterface::class,
66
                    is_object($logger) ? get_class($logger) : gettype($logger),
67
                    $serviceName
68
                )
69
            );
70
        }
71
72 3
        return $logger;
73
    }
74
75
    /**
76
     * @param string $loggerService
77
     */
78
    public function setLoggerService(string $loggerService): void
79
    {
80
        $this->loggerService = $loggerService;
81
    }
82
}
83