Channel::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace FMUP\Logger;
3
4
use FMUP\Config;
5
use FMUP\Environment;
6
use FMUP\Request;
7
use FMUP\Response;
8
use FMUP\Sapi;
9
use Monolog\Logger as MonologLogger;
10
11
abstract class Channel
12
{
13
    use Environment\OptionalTrait {
14
        getEnvironment as getEnvironmentTrait;
15
    }
16
    use Config\OptionalTrait;
17
    use Sapi\OptionalTrait;
18
19
    /**
20
     * @var MonologLogger
21
     */
22
    private $logger;
23
24
    /**
25
     * @var Request
26
     */
27
    private $request;
28
29
    /**
30
     * @var Response
31
     */
32
    private $response;
33
34
    /**
35
     * Get name of the current channel
36
     * @return String
37
     */
38 1
    public function getName()
39
    {
40 1
        $split = explode('\\', get_class($this));
41 1
        return array_pop($split);
42
    }
43
44
    /**
45
     * Must configure the logger channel
46
     * @return MonologLogger
47
     */
48
    abstract public function configure();
49
50
    /**
51
     * Retrieve defined logger
52
     * @return MonologLogger
53
     */
54 1
    public function getLogger()
55
    {
56 1
        if (!isset($this->logger)) {
57 1
            $this->logger = new MonologLogger($this->getName());
58 1
            $this->configure();
59
        }
60 1
        return $this->logger;
61
    }
62
63
    /**
64
     * @param MonologLogger $logger
65
     * @return $this
66
     */
67 1
    public function setLogger(MonologLogger $logger)
68
    {
69 1
        $this->logger = $logger;
70 1
        return $this;
71
    }
72
73
    /**
74
     * @return Environment
75
     * @throws Exception
76
     */
77 1 View Code Duplication
    public function getEnvironment()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79 1
        if (!$this->hasEnvironment()) {
80 1
            $environment = Environment::getInstance();
81 1
            if ($this->hasConfig()) {
82 1
                $environment->setConfig($this->getConfig());
83
            }
84 1
            $this->setEnvironment($environment);
85
        }
86 1
        return $this->getEnvironmentTrait();
87
    }
88
89
    /**
90
     * @param Request $request
91
     * @return $this
92
     */
93 1
    public function setRequest(Request $request)
94
    {
95 1
        $this->request = $request;
96 1
        return $this;
97
    }
98
99 2
    public function getRequest()
100
    {
101 2
        if (!$this->request) {
102 1
            throw new Exception("Request must be defined");
103
        }
104 1
        return $this->request;
105
    }
106
107 1
    public function setResponse(Response $response)
108
    {
109 1
        $this->response = $response;
110 1
        return $this;
111
    }
112
113 2
    public function getResponse()
114
    {
115 2
        if (!$this->response) {
116 1
            throw new Exception('Response must be defined');
117
        }
118 1
        return $this->response;
119
    }
120
121
    /**
122
     * Add a message in logger
123
     * @param $level
124
     * @param $message
125
     * @param array $context
126
     * @return bool
127
     */
128 1
    public function addRecord($level, $message, array $context = array())
129
    {
130 1
        return $this->getLogger()->addRecord((int)$level, (string)$message, (array)$context);
131
    }
132
}
133