Completed
Push — 4.0 ( 2ca4e1...0c3d7c )
by Marco
15:24
created

DispatcherLogger::getLevel()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 42
rs 4.909
cc 9
eloc 28
nc 9
nop 1
1
<?php namespace Comodojo\Dispatcher\Log;
2
3
use \Monolog\Handler\HandlerInterface;
4
use \Monolog\Logger;
5
use \Monolog\Handler\StreamHandler;
6
use \Monolog\Handler\NullHandler;
7
use \Comodojo\Dispatcher\Components\Configuration;
8
9
/**
10
 * @package     Comodojo Dispatcher
11
 * @author      Marco Giovinazzi <[email protected]>
12
 * @author      Marco Castiello <[email protected]>
13
 * @license     GPL-3.0+
14
 *
15
 * LICENSE:
16
 *
17
 * This program is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License as
19
 * published by the Free Software Foundation, either version 3 of the
20
 * License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License
28
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29
 */
30
31
32
class DispatcherLogger {
33
34
    private $configuration;
35
36
    public function __construct(Configuration $configuration) {
37
38
        $this->configuration = $configuration;
39
40
    }
41
42
    public function init() {
43
44
        $log = $this->configuration->get('log');
45
46
        if (
47
            empty($log) ||
48
            ( isset($log['enabled']) && $log['enabled'] === false ) ||
49
            empty($log['providers'])
50
        ) {
51
52
            $logger = new Logger('dispatcher');
53
54
            $logger->pushHandler( new NullHandler( self::getLevel() ) );
55
56
        } else {
57
58
            $name = empty($log['name']) ? 'dispatcher' : $log['name'];
59
60
            $logger = new Logger($name);
61
62
            foreach ($log['providers'] as $provider => $parameters) {
63
64
                $handler = $this->getHandler($provider, $parameters);
65
66
                if ( $handler instanceof HandlerInterface ) $logger->pushHandler($handler);
67
68
            }
69
70
        }
71
72
        return $logger;
73
74
    }
75
76
    /**
77
     * Create the logger
78
     *
79
     * @param Configuration $configuration
80
     *
81
     * @return Logger
82
     */
83
    public static function create(Configuration $configuration) {
84
85
        $log = new DispatcherLogger($configuration);
86
87
        return $log->init();
88
89
    }
90
91
    protected function getHandler($provider, $parameters) {
0 ignored issues
show
Unused Code introduced by
The parameter $provider is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
93
        switch ( strtolower($parameters['type']) ) {
94
95
            case 'streamhandler':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
96
97
                $target = empty($parameters['target']) ? 'dispatcher.log' : $parameters['target'];
98
99
                $file = $this->configuration->get('base-path').'/'.$target;
100
101
                $level = self::getLevel( empty($parameters['level']) ? 'info' : $parameters['level'] );
102
103
                $handler = new StreamHandler($file, $level);
104
105
                break;
106
107
            default:
108
                $handler = null;
109
                break;
110
        }
111
112
        return $handler;
113
114
    }
115
116
    /**
117
     * Map provided log level to level code
118
     *
119
     * @param   string    $level
120
     *
121
     * @return  integer
122
     */
123
    protected static function getLevel($level = null) {
124
125
        switch ( strtoupper($level) ) {
126
127
            case 'INFO':
128
                $logger_level = Logger::INFO;
129
                break;
130
131
            case 'NOTICE':
132
                $logger_level = Logger::NOTICE;
133
                break;
134
135
            case 'WARNING':
136
                $logger_level = Logger::WARNING;
137
                break;
138
139
            case 'ERROR':
140
                $logger_level = Logger::ERROR;
141
                break;
142
143
            case 'CRITICAL':
144
                $logger_level = Logger::CRITICAL;
145
                break;
146
147
            case 'ALERT':
148
                $logger_level = Logger::ALERT;
149
                break;
150
151
            case 'EMERGENCY':
152
                $logger_level = Logger::EMERGENCY;
153
                break;
154
155
            case 'DEBUG':
156
            default:
157
                $logger_level = Logger::DEBUG;
158
                break;
159
160
        }
161
162
        return $logger_level;
163
164
    }
165
166
}
167