Telegram::send()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 23
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 34
rs 9.552
1
<?php 
2
3
namespace Yaro\LogEnvelope\Drivers;
4
5
class Telegram extends AbstractDriver
6
{
7
        
8
    protected function check() 
9
    {
10
        return $this->isEnabled() && (isset($this->config['chats']) && $this->config['chats'] && is_array($this->config['chats']) && isset($this->config['token']) && $this->config['token']);
11
    } // end check
12
    
13
    public function send()
14
    {
15
        if (!$this->check()) {
16
            return;
17
        }
18
        
19
        //https://api.telegram.org/{$this->config['token']}/getUpdates
20
        $data = $this->data;
21
        
22
        $text = sprintf(
23
            '[%s] <b>%s</b>%s[%s] <b>%s</b>%sin <code>%s</code> line %s%s', 
24
            urlencode($data['method']), 
25
            urlencode($data['fullUrl']), 
26
            urlencode("\n \n"),
27
            urlencode($data['class']),
28
            urlencode($data['exception']),
29
            urlencode("\n"),
30
            urlencode($data['file']),
31
            $data['line'],
32
            urlencode("\n \n")
33
        );
34
35
        $biggestNumberLength = strlen(max(array_keys($data['file_lines'])));
36
        foreach ($data['file_lines'] as $num => $line) {
37
            $num = str_pad($num, $biggestNumberLength, ' ', STR_PAD_LEFT);
38
            $num = '<code>'. $num .'</code>';
39
            $text .= urlencode($num .'|<code>'. htmlentities($line) .'</code>');
40
        }
41
42
        foreach ($this->config['chats'] as $idUser) {
43
            $url = 'https://api.telegram.org/'. $this->config['token'] .'/sendMessage?disable_web_page_preview=true&chat_id='. $idUser
44
                 . '&parse_mode=HTML&text=';
45
                 
46
            @file_get_contents($url . $text);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for file_get_contents(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

46
            /** @scrutinizer ignore-unhandled */ @file_get_contents($url . $text);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
47
        }
48
    } // end send
49
    
50
}
51