Logger::setFactory()   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 1
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;
3
4
class Logger
5
{
6
    use Environment\OptionalTrait {
7
        getEnvironment as getEnvironmentTrait;
8
    }
9
    use Config\OptionalTrait;
10
    /**
11
     * Detailed debug information
12
     */
13
    const DEBUG = \Monolog\Logger::DEBUG;
14
15
    /**
16
     * Interesting events
17
     *
18
     * Examples: User logs in, SQL logs.
19
     */
20
    const INFO = \Monolog\Logger::INFO;
21
22
    /**
23
     * Uncommon events
24
     */
25
    const NOTICE = \Monolog\Logger::NOTICE;
26
27
    /**
28
     * Exceptional occurrences that are not errors
29
     *
30
     * Examples: Use of deprecated APIs, poor use of an API,
31
     * undesirable things that are not necessarily wrong.
32
     */
33
    const WARNING = \Monolog\Logger::WARNING;
34
35
    /**
36
     * Runtime errors
37
     */
38
    const ERROR = \Monolog\Logger::ERROR;
39
40
    /**
41
     * Critical conditions
42
     *
43
     * Example: Application component unavailable, unexpected exception.
44
     */
45
    const CRITICAL = \Monolog\Logger::CRITICAL;
46
47
    /**
48
     * Action must be taken immediately
49
     *
50
     * Example: Entire website down, database unavailable, etc.
51
     * This should trigger the SMS alerts and wake you up.
52
     */
53
    const ALERT = \Monolog\Logger::ALERT;
54
55
    /**
56
     * Urgent alert.
57
     */
58
    const EMERGENCY = \Monolog\Logger::EMERGENCY;
59
60
61
    private $instances = array();
62
    /**
63
     * @var Request
64
     */
65
    private $request;
66
67
68
    protected $factory;
69
70
    /**
71
     * @return Logger\Factory
72
     */
73 2
    public function getFactory()
74
    {
75 2
        if (!$this->factory) {
76 2
            $this->factory = Logger\Factory::getInstance();
77
        }
78 2
        return $this->factory;
79
    }
80
81
    /**
82
     * @param Logger\Factory $factory
83
     * @return $this
84
     */
85 1
    public function setFactory(Logger\Factory $factory)
86
    {
87 1
        $this->factory = $factory;
88 1
        return $this;
89
    }
90
91
    /**
92
     * @param string $instanceName
93
     * @return Logger\Channel
94
     */
95 2
    public function get($instanceName)
96
    {
97 2
        $instanceName = (string)$instanceName;
98 2
        if (!isset($this->instances[$instanceName])) {
99 1
            $channel = $this->getFactory()->getChannel($instanceName);
100 1
            $channel->setConfig($this->getConfig())->setEnvironment($this->getEnvironment());
101 1
            $this->instances[$instanceName] = $channel;
102
        }
103 2
        return $this->instances[$instanceName];
104
    }
105
106
    /**
107
     * @param Logger\Channel $logger
108
     * @param string $instanceName
109
     * @return $this
110
     */
111 2
    public function set(Logger\Channel $logger, $instanceName)
112
    {
113 2
        $instanceName = (string)$instanceName;
114 2
        if (!is_null($logger) && !is_null($instanceName)) {
115 2
            $this->instances[$instanceName] = $logger;
116
        }
117 2
        return $this;
118
    }
119
120
    /**
121
     * Define HTTP request object
122
     * @param Request $request
123
     * @return $this
124
     */
125 3
    public function setRequest(Request $request)
126
    {
127 3
        $this->request = $request;
128 3
        return $this;
129
    }
130
131
    /**
132
     * Retrieve defined HTTP request object
133
     * @return Request
134
     * @throws \LogicException if no request has been set
135
     */
136 2
    public function getRequest()
137
    {
138 2
        if (!$this->request) {
139 1
            throw new \LogicException('Request is not defined');
140
        }
141 1
        return $this->request;
142
    }
143
144
    /**
145
     * @return Environment
146
     */
147 2 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...
148
    {
149 2
        if (!$this->hasEnvironment()) {
150 2
            $environment = Environment::getInstance();
151 2
            $this->setEnvironment($environment->setConfig($this->getConfig()));
152
        }
153 2
        return $this->getEnvironmentTrait();
154
    }
155
156
    /**
157
     * Add log Record
158
     * @param string $channel
159
     * @param int $level
160
     * @param string $message
161
     * @param array $context
162
     * @return bool
163
     */
164 1
    public function log($channel, $level, $message, array $context = array())
165
    {
166 1
        $channel = (string)$channel;
167 1
        $message = (string)$message;
168 1
        $level = (int)($level <= 0 ? self::ALERT : $level);
169 1
        $channelType = $this->get($channel);
170 1
        if ($channelType->getName() === Logger\Channel\Standard::NAME) {
171 1
            $message = "[Channel $channel] $message";
172
        }
173 1
        return $channelType->addRecord((int)$level, $message, (array)$context);
174
    }
175
}
176