TelegramHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 26
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A write() 0 4 1
1
<?php
2
3
namespace TelegramHandler;
4
use Monolog\Logger;
5
use Monolog\Handler\AbstractProcessingHandler;
6
use TelegramHandler\TelegramAdapter;
7
8
/**
9
 * TelegramHandler
10
 *
11
 * @example 
12
 *    $log = new Logger('application');
13
 *    $telegram = new TelegramHandler('123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11', 123456789);
14
 *    $log->pushHandler($telegram);
15
 * 
16
 * @author Renan Melo <[email protected]>
17
 * @author Rafael Nery <[email protected]>
18
 *
19
 * @see AbstractProcessingHandler
20
 */
21
class TelegramHandler extends AbstractProcessingHandler {
22
23
    private $telegram;
24
25
    
26
    /**
27
     * @param String      $token   Bot Token (Hint: BotFather)
28
     * @param Integer     $chatId  ChatId (Hint: Retrieve via https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/getUpdates)
29
     * @param Intger      $level   The minimum logging level at which this handler will be triggered
30
     * @param Boolean     $bubble  Whether the messages that are handled can bubble up the stack or not
31
     */
32
    public function __construct($token, $chatId, $level = Logger::DEBUG, $bubble = true) 
33
    {
34
35
      parent::__construct($level, $bubble);
36
      $this->telegram = new TelegramAdapter($token, $chatId);
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    protected function write(array $record)
43
    {
44
      $this->telegram->sendMessage($record['formatted']);
45
    }
46
}
47