Failed Conditions
Pull Request — master (#11)
by Adrien
15:48 queued 12:33
created

LoggerFactory::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 13
c 3
b 0
f 0
nc 3
nop 3
dl 0
loc 28
cc 3
rs 9.8333
ccs 14
cts 14
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Log;
6
7
use Ecodev\Felix\Log\Writer\Db;
1 ignored issue
show
Bug introduced by
The type Ecodev\Felix\Log\Writer\Db was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Ecodev\Felix\Log\Writer\Mail;
9
use Interop\Container\ContainerInterface;
10
use Laminas\Log\Logger;
11
use Laminas\Log\Writer\Stream;
12
use Laminas\ServiceManager\Factory\FactoryInterface;
13
14
final class LoggerFactory implements FactoryInterface
15
{
16
    private ?Logger $logger = null;
17
18
    /**
19
     * @param string $requestedName
20
     */
21 2
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): Logger
22
    {
23 2
        if (!$this->logger) {
24 2
            $this->logger = new Logger();
25
26
            // Log to file
27 2
            $fileWriter = new Stream('logs/all.log');
28 2
            $this->logger->addWriter($fileWriter);
29
30
            // Log to DB
31
            /** @var Db $dbWriter */
32 2
            $dbWriter = $container->get(Db::class);
33 2
            $dbWriter->addFilter(Logger::INFO);
34 2
            $this->logger->addWriter($dbWriter);
35
36
            // Maybe log to emails
37
            /** @var null|Mail $mailWriter */
38 2
            $mailWriter = $container->get(Mail::class);
39 2
            if ($mailWriter) {
40 1
                $this->logger->addWriter($mailWriter);
41
            }
42
43
            // Register to log all kind of PHP errors
44 2
            Logger::registerErrorHandler($this->logger, true);
45 2
            Logger::registerFatalErrorShutdownFunction($this->logger);
46
        }
47
48 2
        return $this->logger;
49
    }
50
}
51