Completed
Pull Request — master (#18)
by Ruben
04:55
created

LoggerAwareTrait::getLogger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace MovingImage\Client\VMPro\Util\Logging\Traits;
4
5
use Monolog\Handler\NullHandler;
6
use Monolog\Logger;
7
use Psr\Log\LoggerInterface;
8
9
/**
10
 * Trait that abstracts implementing of LoggerAwareInterface setter method
11
 * as well as logger storage + retrieval within a class.
12
 *
13
 * @author Ruben Knol <[email protected]>
14
 */
15
trait LoggerAwareTrait
16
{
17
    /**
18
     * @var LoggerInterface
19
     */
20
    private $logger;
21
22
    /**
23
     * Method to inject PSR logger into this class.
24
     *
25
     * @param LoggerInterface $logger
26
     */
27
    public function setLogger(LoggerInterface $logger)
28
    {
29
        $this->logger = $logger;
30
    }
31
32
    /**
33
     * Get the logger instance associated with this instance.
34
     *
35
     * @return LoggerInterface
36
     */
37
    protected function getLogger()
38
    {
39
        if (!isset($this->logger)) {
40
            // When no logger is injected, create a new one
41
            // that doesn't do anything
42
            $this->logger = new Logger('api-client');
43
            $this->logger->setHandlers([
44
                new NullHandler(),
45
            ]);
46
        }
47
48
        return $this->logger;
49
    }
50
}
51