Completed
Push — 4.0 ( 89799b...3c6496 )
by Marco
02:53
created

LogManager::getBubble()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 4
nc 1
nop 1
1
<?php namespace Comodojo\Dispatcher\Components;
2
3
use \Monolog\Handler\HandlerInterface;
4
use \Monolog\Logger;
5
use \Monolog\Handler\StreamHandler;
6
use \Monolog\Handler\SyslogHandler;
7
use \Monolog\Handler\ErrorLogHandler;
8
use \Monolog\Handler\NullHandler;
9
use \Comodojo\Dispatcher\Components\Configuration;
10
11
/**
12
 * @package     Comodojo Dispatcher
13
 * @author      Marco Giovinazzi <[email protected]>
14
 * @author      Marco Castiello <[email protected]>
15
 * @license     GPL-3.0+
16
 *
17
 * LICENSE:
18
 *
19
 * This program is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License as
21
 * published by the Free Software Foundation, either version 3 of the
22
 * License, or (at your option) any later version.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License
30
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31
 */
32
33
34
class LogManager {
35
36
    private $configuration;
37
38
    public function __construct(Configuration $configuration) {
39
40
        $this->configuration = $configuration;
41
42
    }
43
44
    public function init() {
45
46
        $log = $this->configuration->get('log');
47
48
        if (
49
            empty($log) ||
50
            ( isset($log['enabled']) && $log['enabled'] === false ) ||
51
            empty($log['providers'])
52
        ) {
53
54
            $logger = new Logger('dispatcher');
55
56
            $logger->pushHandler( new NullHandler( self::getLevel() ) );
57
58
        } else {
59
60
            $name = empty($log['name']) ? 'dispatcher' : $log['name'];
61
62
            $logger = new Logger($name);
63
64
            foreach ($log['providers'] as $provider => $parameters) {
65
66
                $handler = $this->getHandler($provider, $parameters);
67
68
                if ( $handler instanceof HandlerInterface ) $logger->pushHandler($handler);
69
70
            }
71
72
        }
73
74
        return $logger;
75
76
    }
77
78
    /**
79
     * Create the logger
80
     *
81
     * @param Configuration $configuration
82
     *
83
     * @return Logger
84
     */
85
    public static function create(Configuration $configuration) {
86
87
        $log = new DispatcherLogger($configuration);
88
89
        return $log->init();
90
91
    }
92
93
    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...
94
95
        switch ( strtolower($parameters['type']) ) {
96
97
            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...
98
                
99
                $stream = $this->configuration->get('base-path').'/'.(empty($parameters['stream']) ? 'dispatcher.log' : $parameters['stream']);
100
101
                $level = self::getLevel( empty($parameters['level']) ? null : $parameters['level'] );
102
                
103
                $bubble = self::getBubble( empty($parameters['bubble']) ? true : $parameters['bubble'] );
104
                
105
                $filePermission = self::getFilePermission( empty($parameters['filePermission']) ? null : $parameters['filePermission'] );
106
                
107
                $useLocking = self::getLocking( empty($parameters['useLocking']) ? false : $parameters['useLocking'] );
108
109
                $handler = new StreamHandler($stream, $level, $bubble, $filePermission, $useLocking);
110
111
                break;
112
                
113
            case 'SyslogHandler':
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...
114
                
115
                if ( empty($parameters['ident']) ) return null;
116
                
117
                $facility = empty($parameters['facility']) ? LOG_USER : $parameters['facility'];
118
            
119
                $level = self::getLevel( empty($parameters['level']) ? null : $parameters['level'] );
120
            
121
                $bubble = self::getBubble( empty($parameters['bubble']) ? true : $parameters['bubble'] );
122
            
123
                $logopts = empty($parameters['logopts']) ? LOG_PID : $parameters['logopts'];
124
            
125
                $handler = new SyslogHandler($parameters['ident'], $facility, $level, $bubble, $logopts);
126
                
127
                break;
128
                
129
            case 'ErrorLogHandler':
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...
130
                
131
                $messageType = empty($parameters['messageType']) ? ErrorLogHandler::OPERATING_SYSTEM : $parameters['messageType'];
132
                
133
                $level = self::getLevel( empty($parameters['level']) ? null : $parameters['level'] );
134
                
135
                $bubble = self::getBubble( empty($parameters['bubble']) ? true : $parameters['bubble'] );
136
                
137
                $expandNewlines = self::getExpandNewlines( empty($parameters['expandNewlines']) ? false : $parameters['expandNewlines'] );
138
                
139
                $handler = new ErrorLogHandler($messageType, $level, $bubble, $expandNewlines);
140
                
141
                break;
142
                
143
            default:
144
                $handler = null;
145
                break;
146
        }
147
148
        return $handler;
149
150
    }
151
152
    /**
153
     * Map provided log level to level code
154
     *
155
     * @param   string    $level
156
     *
157
     * @return  integer
158
     */
159
    protected static function getLevel($level = null) {
160
161
        switch ( strtoupper($level) ) {
162
163
            case 'INFO':
164
                $logger_level = Logger::INFO;
165
                break;
166
167
            case 'NOTICE':
168
                $logger_level = Logger::NOTICE;
169
                break;
170
171
            case 'WARNING':
172
                $logger_level = Logger::WARNING;
173
                break;
174
175
            case 'ERROR':
176
                $logger_level = Logger::ERROR;
177
                break;
178
179
            case 'CRITICAL':
180
                $logger_level = Logger::CRITICAL;
181
                break;
182
183
            case 'ALERT':
184
                $logger_level = Logger::ALERT;
185
                break;
186
187
            case 'EMERGENCY':
188
                $logger_level = Logger::EMERGENCY;
189
                break;
190
191
            case 'DEBUG':
192
            default:
193
                $logger_level = Logger::DEBUG;
194
                break;
195
196
        }
197
198
        return $logger_level;
199
200
    }
201
202
    protected static function getBubble($bubble) {
203
        
204
        return filter_var($bubble, FILTER_VALIDATE_BOOLEAN, array(
205
            'options' => array(
206
                'default' => true
207
            )
208
        ));
209
        
210
    }
211
    
212
    protected static function getFilePermission($filepermission = null) {
213
        
214
        if ( is_null($filepermission) ) return null;
215
        
216
        return filter_var($bubble, FILTER_VALIDATE_INT, array(
0 ignored issues
show
Bug introduced by
The variable $bubble does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
217
            'options' => array(
218
                'default' => 0644
219
            ),
220
            'flags' => FILTER_FLAG_ALLOW_OCTAL
221
        ));
222
        
223
    }
224
225
    protected static function getLocking($uselocking) {
226
        
227
        return filter_var($uselocking, FILTER_VALIDATE_BOOLEAN, array(
228
            'options' => array(
229
                'default' => false
230
            )
231
        ));
232
        
233
    }
234
    
235
    protected static function getExpandNewlines($expandNewlines) {
236
        
237
        return filter_var($expandNewlines, FILTER_VALIDATE_BOOLEAN, array(
238
            'options' => array(
239
                'default' => false
240
            )
241
        ));
242
        
243
    }
244
245
}
246