Logger::getLogger()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace Infuse\Services;
12
13
use Monolog\ErrorHandler;
14
use Monolog\Handler\NullHandler;
15
use Monolog\Processor\IntrospectionProcessor;
16
use Monolog\Processor\WebProcessor;
17
use Monolog\Logger as Monolog;
18
19
class Logger
20
{
21
    /**
22
     * @var Monolog\Logger
23
     */
24
    private $logger;
25
26
    public function __construct($app)
27
    {
28
        // Install the PHP error handler now only if logging
29
        // is enabled. Otherwise we don't need to instantiate the
30
        // logger until it has been requested through the DI container.
31
        if ($this->hasLogging($app)) {
32
            ErrorHandler::register($this->getLogger($app));
33
        }
34
    }
35
36
    public function __invoke($app)
37
    {
38
        return $this->getLogger($app);
39
    }
40
41
    private function hasLogging($app)
42
    {
43
        return $app['config']->get('logger.enabled');
44
    }
45
46
    private function getLogger($app)
47
    {
48
        if (!$this->logger) {
49
            $handlers = [];
50
            $config = $app['config'];
51
52
            if ($this->hasLogging($app)) {
53
                $webProcessor = new WebProcessor();
54
                $webProcessor->addExtraField('user_agent', 'HTTP_USER_AGENT');
55
56
                $processors = [
57
                    $webProcessor,
58
                    new IntrospectionProcessor(),
59
                ];
60
            } else {
61
                $processors = [];
62
                $handlers[] = new NullHandler();
63
            }
64
65
            $this->logger = new Monolog($config->get('app.hostname'), $handlers, $processors);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Monolog\Logger($con...$handlers, $processors) of type object<Monolog\Logger> is incompatible with the declared type object<Monolog\Logger\Logger> of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
        }
67
68
        return $this->logger;
69
    }
70
}
71