LoggerFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
eloc 12
dl 0
loc 32
rs 10
c 3
b 0
f 0
ccs 11
cts 11
cp 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 25 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Log;
6
7
use Ecodev\Felix\Log\Handler\DbHandler;
8
use Ecodev\Felix\Log\Handler\MailerHandler;
9
use Laminas\ServiceManager\Factory\FactoryInterface;
10
use Monolog\ErrorHandler;
11
use Monolog\Handler\StreamHandler;
12
use Monolog\Logger;
13
use Psr\Container\ContainerInterface;
14
use Psr\Log\LoggerInterface;
15
16
final class LoggerFactory implements FactoryInterface
17
{
18
    private ?Logger $logger = null;
19
20
    /**
21
     * @param string $requestedName
22
     */
23 2
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): LoggerInterface
24
    {
25 2
        if (!$this->logger) {
26 2
            $this->logger = new Logger('app');
27
28
            // Log to file
29 2
            $this->logger->pushHandler(new StreamHandler('logs/all.log'));
30
31
            // Log to DB
32
            /** @var DbHandler $dbHandler */
33 2
            $dbHandler = $container->get(DbHandler::class);
34 2
            $this->logger->pushHandler($dbHandler);
35
36
            // Maybe log to emails
37
            /** @var null|MailerHandler $mailHandler */
38 2
            $mailHandler = $container->get(MailerHandler::class);
39 2
            if ($mailHandler) {
40 1
                $this->logger->pushHandler($mailHandler);
41
            }
42
43
            // Register to log all kinds of PHP errors
44 2
            ErrorHandler::register($this->logger);
45
        }
46
47 2
        return $this->logger;
48
    }
49
}
50