Completed
Pull Request — develop (#412)
by
unknown
02:56
created

TelegramLog::isUpdateLogActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot;
12
13
use Longman\TelegramBot\Exception\TelegramLogException;
14
use Monolog\Formatter\LineFormatter;
15
use Monolog\Handler\HandlerInterface;
16
use Monolog\Handler\StreamHandler;
17
use Monolog\Logger;
18
19
class TelegramLog
20
{
21
    /**
22
     * Monolog instance
23
     *
24
     * @var Logger
25
     */
26
    static protected $monolog;
27
28
    /**
29
     * Monolog instance for update
30
     *
31
     * @var Logger
32
     */
33
    static protected $monolog_update;
34
35
    /**
36
     * Path for error log
37
     *
38
     * @var string
39
     */
40
    static protected $error_log_path;
41
42
    /**
43
     * Path for debug log
44
     *
45
     * @var string
46
     */
47
    static protected $debug_log_path;
48
49
    /**
50
     * Path for update log
51
     *
52
     * @var string
53
     */
54
    static protected $update_log_path;
55
56
    /**
57
     * Temporary stream handle for debug log
58
     *
59
     * @var resource|null
60
     */
61
    static protected $debug_log_temp_stream_handle;
62
63
    /**
64
     * Initialize
65
     *
66
     * Initilize monolog instance. Singleton
67
     * Is possbile provide an external monolog instance
68
     *
69
     * @param Logger $external_monolog
70
     *
71
     * @return Logger
72
     */
73 3
    public static function initialize(Logger $external_monolog = null)
74
    {
75 3
        if (self::$monolog === null) {
76 3
            if ($external_monolog !== null) {
77 1
                self::$monolog = $external_monolog;
78
            } else {
79 2
                self::$monolog = new Logger('bot_log');
80
            }
81
        }
82
83 3
        return self::$monolog;
84
    }
85
86
    /**
87
     * Initialize update
88
     *
89
     * Initilize monolog update instance. Singleton
90
     * Is possbile provide an external monolog instance
91
     *
92
     * @param Logger $external_monolog
93
     *
94
     * @return Logger
95
     */
96 1
    public static function initializeUpdate(Logger $external_monolog = null)
97
    {
98 1
        if (self::$monolog_update === null) {
99 1
            if ($external_monolog !== null) {
100
                self::$monolog_update = $external_monolog;
101
            } else {
102 1
                self::$monolog_update = new Logger('bot_update_log');
103
            }
104
        }
105
106 1
        return self::$monolog_update;
107
    }
108
109
    /**
110
     * Initialize error log
111
     *
112
     * @param string           $path
113
     * @param HandlerInterface $external_handler
114
     *
115
     * @return Logger
116
     * @throws TelegramLogException
117
     */
118 2 View Code Duplication
    public static function initErrorLog($path, HandlerInterface $external_handler = null)
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...
119
    {
120 2
        if (($path === null || $path === '') && is_null($external_handler)) {
121 1
            throw new TelegramLogException('Empty path for error log');
122
        }
123 1
        self::initialize();
124
125 1
        if (is_null($external_handler)) {
126 1
            self::$error_log_path = $path;
127 1
            $handler = new StreamHandler(self::$error_log_path, Logger::ERROR);
128
        } else {
129
            self::$error_log_path = 'true';
130
            $handler = $external_handler;
131
        }
132
133 1
        return self::$monolog->pushHandler($handler->setFormatter(new LineFormatter(null, null, true)));
134
    }
135
136
    /**
137
     * Initialize debug log
138
     *
139
     * @param string           $path
140
     * @param HandlerInterface $external_handler
141
     *
142
     * @return Logger
143
     * @throws TelegramLogException
144
     */
145 2 View Code Duplication
    public static function initDebugLog($path, HandlerInterface $external_handler = null)
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...
146
    {
147 2
        if (($path === null || $path === '') && is_null($external_handler)) {
148 1
            throw new TelegramLogException('Empty path for debug log');
149
        }
150 1
        self::initialize();
151
152 1
        if (is_null($external_handler)) {
153 1
            self::$debug_log_path = $path;
154 1
            $handler = new StreamHandler(self::$debug_log_path, Logger::DEBUG);
155
        } else {
156
            self::$debug_log_path = 'true';
157
            $handler = $external_handler;
158
        }
159
160 1
        return self::$monolog->pushHandler($handler->setFormatter(new LineFormatter(null, null, true)));
161
    }
162
163
    /**
164
     * Get the stream handle of the temporary debug output
165
     *
166
     * @return mixed The stream if debug is active, else false
167
     */
168
    public static function getDebugLogTempStream()
169
    {
170
        if (self::$debug_log_temp_stream_handle === null) {
171
            if (!self::isDebugLogActive()) {
172
                return false;
173
            }
174
            self::$debug_log_temp_stream_handle = fopen('php://temp', 'w+b');
175
        }
176
177
        return self::$debug_log_temp_stream_handle;
178
    }
179
180
    /**
181
     * Write the temporary debug stream to log and close the stream handle
182
     *
183
     * @param string $message Message (with placeholder) to write to the debug log
184
     */
185
    public static function endDebugLogTempStream($message = '%s')
186
    {
187
        if (is_resource(self::$debug_log_temp_stream_handle)) {
188
            rewind(self::$debug_log_temp_stream_handle);
189
            self::debug($message, stream_get_contents(self::$debug_log_temp_stream_handle));
190
            fclose(self::$debug_log_temp_stream_handle);
191
            self::$debug_log_temp_stream_handle = null;
192
        }
193
    }
194
195
    /**
196
     * Initialize update log
197
     *
198
     * Initilize monolog instance. Singleton
199
     * Is possbile provide an external monolog instance
200
     *
201
     * @param string           $path
202
     * @param HandlerInterface $external_handler
203
     *
204
     * @return Logger
205
     * @throws TelegramLogException
206
     */
207 2
    public static function initUpdateLog($path, HandlerInterface $external_handler = null)
208
    {
209 2
        if (($path === null || $path === '') && is_null($external_handler)) {
210 1
            throw new TelegramLogException('Empty path for update log');
211
        }
212 1
        self::initializeUpdate();
213
214 1
        if (is_null($external_handler)) {
215 1
            self::$update_log_path = $path;
216 1
            $handler = new StreamHandler(self::$update_log_path, Logger::INFO);
217
        } else {
218
            self::$update_log_path = 'true';
219
            $handler = $external_handler;
220
        }
221
222 1
        return self::$monolog_update->pushHandler($handler->setFormatter(new LineFormatter('%message%' . PHP_EOL)));
223
    }
224
225
    /**
226
     * Is error log active
227
     *
228
     * @return bool
229
     */
230 4
    public static function isErrorLogActive()
231
    {
232 4
        return self::$error_log_path !== null;
233
    }
234
235
    /**
236
     * Is debug log active
237
     *
238
     * @return bool
239
     */
240 2
    public static function isDebugLogActive()
241
    {
242 2
        return self::$debug_log_path !== null;
243
    }
244
245
    /**
246
     * Is update log active
247
     *
248
     * @return bool
249
     */
250 1
    public static function isUpdateLogActive()
251
    {
252 1
        return self::$update_log_path !== null;
253
    }
254
255
    /**
256
     * Report error log
257
     *
258
     * @param string $text
259
     */
260 4
    public static function error($text)
261
    {
262 4
        if (self::isErrorLogActive()) {
263 4
            $text = self::getLogText($text, func_get_args());
264 4
            self::$monolog->error($text);
265
        }
266 4
    }
267
268
    /**
269
     * Report debug log
270
     *
271
     * @param string $text
272
     */
273 2
    public static function debug($text)
274
    {
275 2
        if (self::isDebugLogActive()) {
276 2
            $text = self::getLogText($text, func_get_args());
277 2
            self::$monolog->debug($text);
278
        }
279 2
    }
280
281
    /**
282
     * Report update log
283
     *
284
     * @param string $text
285
     */
286 1
    public static function update($text)
287
    {
288 1
        if (self::isUpdateLogActive()) {
289 1
            $text = self::getLogText($text, func_get_args());
290 1
            self::$monolog_update->info($text);
291
        }
292 1
    }
293
294
    /**
295
     * Applies vsprintf to the text if placeholder replacements are passed along.
296
     *
297
     * @param string $text
298
     * @param array  $args
299
     *
300
     * @return string
301
     */
302 6
    protected static function getLogText($text, array $args = [])
303
    {
304
        // Pop the $text off the array, as it gets passed via func_get_args().
305 6
        array_shift($args);
306
307
        // Suppress warning if placeholders don't match out.
308 6
        return @vsprintf($text, $args) ?: $text;
309
    }
310
}
311