GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Monolog   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 216
rs 10
c 0
b 0
f 0
wmc 18

10 Methods

Rating   Name   Duplication   Size   Complexity  
A checkHandlerInfos() 0 4 1
A checkHandlerName() 0 20 4
A getConfig() 0 3 1
A addNewHandler() 0 9 1
A getLogger() 0 3 1
A getHandlers() 0 3 1
A getChannelName() 0 3 1
A __construct() 0 5 1
A addAllHandlers() 0 19 4
A checkHandlerArgs() 0 8 3
1
<?php
2
3
namespace BFW;
4
5
use \Exception;
6
7
/**
8
 * Class to read monolog config file and instanciate monolog from config
9
 */
10
class Monolog
11
{
12
    /**
13
     * @const ERR_HANDLERS_LIST_FORMAT Exception code if config for handlers
14
     * list have not a correct format
15
     */
16
    const ERR_HANDLERS_LIST_FORMAT = 1105001;
17
    
18
    /**
19
     * @const ERR_HANDLER_INFOS_MISSING_NAME Exception code if a handler not
20
     * have declared name
21
     */
22
    const ERR_HANDLER_INFOS_MISSING_NAME = 1105002;
23
    
24
    /**
25
     * @const ERR_HANDLER_NAME_NOT_A_STRING Exception code if a handler name
26
     * value is not a string
27
     */
28
    const ERR_HANDLER_NAME_NOT_A_STRING = 1105003;
29
    
30
    /**
31
     * @const ERR_HANDLER_CLASS_NOT_FOUND Exception code if a handler class
32
     * has not been found
33
     */
34
    const ERR_HANDLER_CLASS_NOT_FOUND = 1105004;
35
    
36
    /**
37
     * @var string $channelName The monolog channel name
38
     */
39
    protected $channelName = '';
40
    
41
    /**
42
     * @var \BFW\Config $config The config object containing the handlers list
43
     */
44
    protected $config;
45
    
46
    /**
47
     * @var \Monolog\Logger $logger The monolog logger object
48
     */
49
    protected $logger;
50
    
51
    /**
52
     * @var array $handlers List of all declared handler
53
     */
54
    protected $handlers = [];
55
    
56
    /**
57
     * Populate properties
58
     * Initialize monolog logger object
59
     * 
60
     * @param string $channelName The monolog channel name
61
     * @param \BFW\Config $config The config object containing handlers list
62
     */
63
    public function __construct(string $channelName, \BFW\Config $config)
64
    {
65
        $this->channelName = $channelName;
66
        $this->config      = $config;
67
        $this->logger      = new \Monolog\Logger($this->channelName);
68
    }
69
    
70
    /**
71
     * Get accessor to property channelName
72
     * 
73
     * @return string
74
     */
75
    public function getChannelName(): string
76
    {
77
        return $this->channelName;
78
    }
79
    
80
    /**
81
     * Get accessor to property config
82
     * 
83
     * @return \BFW\Config
84
     */
85
    public function getConfig(): \BFW\Config
86
    {
87
        return $this->config;
88
    }
89
    
90
    /**
91
     * Get accessor to property logger
92
     * 
93
     * @return \Monolog\Logger
94
     */
95
    public function getLogger(): \Monolog\Logger
96
    {
97
        return $this->logger;
98
    }
99
    
100
    /**
101
     * Get accessor to property handlers
102
     * 
103
     * @return array
104
     */
105
    public function getHandlers(): array
106
    {
107
        return $this->handlers;
108
    }
109
    
110
    /**
111
     * Adding all handlers to monolog logger
112
     * 
113
     * @param string $configKeyName The key name containing handlers list
114
     * @param string $configFileName The config file name containing handlers
115
     * list
116
     * 
117
     * @throws \Exception
118
     * 
119
     * @return void
120
     */
121
    public function addAllHandlers(
122
        string $configKeyName = 'handlers',
123
        string $configFileName = 'monolog.php'
124
    ) {
125
        $handlers = $this->config->getValue($configKeyName, $configFileName);
126
        
127
        if (!is_array($handlers)) {
128
            throw new Exception(
129
                'Handlers list into monolog config file should be an array.',
130
                self::ERR_HANDLERS_LIST_FORMAT
131
            );
132
        }
133
        
134
        if (array_key_exists('name', $handlers)) {
135
            $handlers = [$handlers];
136
        }
137
        
138
        foreach ($handlers as $handlerInfos) {
139
            $this->addNewHandler($handlerInfos);
140
        }
141
    }
142
    
143
    /**
144
     * Check and add a new handler to the logger
145
     * 
146
     * @param array $handlerInfos Handler infos (name and args)
147
     * 
148
     * @throws \Exception
149
     * 
150
     * @return void
151
     */
152
    public function addNewHandler(array $handlerInfos)
153
    {
154
        $this->checkHandlerInfos($handlerInfos);
155
        
156
        $handlerClassName = $handlerInfos['name'];
157
        $handler          = new $handlerClassName(...$handlerInfos['args']);
158
        
159
        $this->handlers[] = $handler;
160
        $this->logger->pushHandler($handler);
161
    }
162
    
163
    /**
164
     * Check the handler infos
165
     * 
166
     * @param array $handlerInfos Handler infos (name and args)
167
     * 
168
     * @throws \Exception
169
     * 
170
     * @return void
171
     */
172
    protected function checkHandlerInfos(array $handlerInfos)
173
    {
174
        $this->checkHandlerName($handlerInfos);
175
        $this->checkHandlerArgs($handlerInfos);
176
    }
177
    
178
    /**
179
     * Check the handler name
180
     * 
181
     * @param array $handlerInfos Handler infos (name and args)
182
     * 
183
     * @throws \Exception
184
     * 
185
     * @return void
186
     */
187
    protected function checkHandlerName(array $handlerInfos)
188
    {
189
        if (!array_key_exists('name', $handlerInfos)) {
190
            throw new Exception(
191
                'The handler infos should have the property name',
192
                self::ERR_HANDLER_INFOS_MISSING_NAME
193
            );
194
        }
195
        
196
        if (!is_string($handlerInfos['name'])) {
197
            throw new Exception(
198
                'The handler name should be a string.',
199
                self::ERR_HANDLER_NAME_NOT_A_STRING
200
            );
201
        }
202
        
203
        if (!class_exists($handlerInfos['name'])) {
204
            throw new Exception(
205
                'The class '.$handlerInfos['name'].' has not been found.',
206
                self::ERR_HANDLER_CLASS_NOT_FOUND
207
            );
208
        }
209
    }
210
    
211
    /**
212
     * Check the handler arguments list
213
     * 
214
     * @param array $handlerInfos Handler infos (name and args)
215
     * 
216
     * @return void
217
     */
218
    protected function checkHandlerArgs(array &$handlerInfos)
219
    {
220
        if (!array_key_exists('args', $handlerInfos)) {
221
            $handlerInfos['args'] = [];
222
        }
223
        
224
        if (!is_array($handlerInfos['args'])) {
225
            $handlerInfos['args'] = [$handlerInfos['args']];
226
        }
227
    }
228
}
229