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

LoggerAwareTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setLogger() 0 4 1
A getLogger() 0 13 2
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