Logs   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A logger() 0 8 2
B setLogger() 0 12 6
A setEchoLogger() 0 6 1
A removeLogger() 0 6 1
1
<?php
2
3
namespace Arrilot\Logs;
4
5
use InvalidArgumentException;
6
use Monolog\Registry;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\NullLogger;
9
10
trait Logs
11
{
12
    /**
13
     * @var LoggerInterface|null
14
     */
15
    protected $logger;
16
17
    /**
18
     * Getter for logger.
19
     * If logger is not set, an null (empty) logger is set. It does not log anywhere.
20
     *
21
     * @return LoggerInterface
22
     */
23
    public function logger()
24
    {
25
        if (is_null($this->logger)) {
26
            $this->logger = new NullLogger();
27
        }
28
        
29
        return $this->logger;
30
    }
31
32
    /**
33
     * Setter for logger.
34
     *
35
     * @param LoggerInterface|string $logger
36
     * @return $this
37
     */
38
    public function setLogger($logger)
39
    {
40
        if (is_object($logger) && $logger instanceof LoggerInterface) {
41
            $this->logger = $logger;
42
        } elseif (is_string($logger) && class_exists('\\Monolog\\Registry') && Registry::hasLogger($logger)) {
43
            $this->logger = Registry::getInstance($logger);
44
        } else {
45
            throw new InvalidArgumentException('Only "Psr\Log\LoggerInterface" or a name for logger in a "Monolog\Registry" are allowed to be passed as $logger');
46
        }
47
48
        return $this;
49
    }
50
    
51
    /**
52
     * Sets a logger that simply echoes everything.
53
     *
54
     * @return $this
55
     */
56
    public function setEchoLogger()
57
    {
58
        $this->logger = new EchoLogger();
59
60
        return $this;
61
    }
62
63
    /**
64
     * Remove a logger.
65
     *
66
     * @return $this
67
     */
68
    public function removeLogger()
69
    {
70
        $this->logger = null;
71
72
        return $this;
73
    }
74
}
75