LoggerProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the Commander project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Commander\Provider;
9
10
use GravityMedia\Commander\Commander;
11
use GravityMedia\Commander\Config;
12
use Monolog\Handler\HandlerInterface;
13
use Monolog\Handler\StreamHandler;
14
use Monolog\Logger;
15
use Psr\Log\LoggerInterface;
16
17
/**
18
 * Logger provider class.
19
 *
20
 * @package GravityMedia\Commander\Provider
21
 */
22
class LoggerProvider
23
{
24
    /**
25
     * @var Config
26
     */
27
    protected $config;
28
29
    /**
30
     * The handlers.
31
     *
32
     * @var HandlerInterface[]
33
     */
34
    protected $handlers;
35
36
    /**
37
     * The logger.
38
     *
39
     * @var null|LoggerInterface
40
     */
41
    protected $logger;
42
43
    /**
44
     * Create commander object.
45
     *
46
     * @param Config $config
47
     */
48 4
    public function __construct(Config $config)
49
    {
50 4
        $this->config = $config;
51 4
    }
52
53
    /**
54
     * Get handlers.
55
     *
56
     * @return HandlerInterface[]
57
     */
58 4
    public function getHandlers()
59
    {
60 4
        if (null === $this->handlers) {
61 4
            $handlers = [];
62
63 4
            $path = $this->config->getLogFilePath();
64 4
            if (null !== $path) {
65 2
                $handlers[] = new StreamHandler($path);
66 1
            }
67
68 4
            $this->handlers = $handlers;
69 2
        }
70
71 4
        return $this->handlers;
72
    }
73
74
    /**
75
     * Get logger.
76
     *
77
     * @return null|LoggerInterface
78
     */
79 4
    public function getLogger()
80
    {
81 4
        if (null === $this->logger) {
82 4
            $logger = new Logger(Commander::LOGGER_NAME);
83
84 4
            foreach ($this->getHandlers() as $handler) {
85 2
                $logger->pushHandler($handler);
86 2
            }
87
88 4
            if (0 === count($logger->getHandlers())) {
89 2
                return null;
90
            }
91
92 2
            $this->logger = $logger;
93 1
        }
94
95 2
        return $this->logger;
96
    }
97
}
98