Slack::send()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 45
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 31
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 45
rs 9.424
1
<?php 
2
3
namespace Yaro\LogEnvelope\Drivers;
4
5
class Slack extends AbstractDriver
6
{
7
        
8
    protected function check() 
9
    {
10
        return $this->isEnabled() && (isset($this->config['channel']) && $this->config['channel'] && 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
        $data = $this->data;
20
        
21
        $text = sprintf(
22
            '[%s] %s%s[%s] *%s*%sin %s line %s%s', 
23
            $data['method'], 
24
            $data['fullUrl'], 
25
            "\n",
26
            $data['class'],
27
            $data['exception'],
28
            "\n",
29
            $data['file'],
30
            $data['line'],
31
            "\n \n"
32
        );
33
34
        $attachments = [
35
            'fallback' => $text,
36
            'color'    => '#55688a',
37
            'text'     => '',
38
        ];
39
        $biggestNumberLength = strlen(max(array_keys($data['file_lines'])));
40
        foreach ($data['file_lines'] as $num => $line) {
41
            $num = str_pad($num, $biggestNumberLength, ' ', STR_PAD_LEFT);
42
            // to be sure that we'll have no extra endings
43
            $line = preg_replace('~[\r\n]~', '', $line);
44
            // double spaces, so in slack it'll be more readable
45
            $line = preg_replace('~(\s+)~', "$1$1", $line);
46
            $attachments['text'] .= $num .'| '. $line ."\n";
47
        }
48
49
50
        $url = 'https://slack.com/api/chat.postMessage?'
51
             . 'token='. $this->config['token']
52
             . '&channel='. urlencode($this->config['channel'])
53
             . '&text='. urlencode($text)
54
             . '&username='. urlencode($this->config['username']) 
55
             . '&as_user=false&icon_url=http%3A%2F%2Fcherry-pie.co%2Fimg%2Flog-envelope.png&mrkdwn=1&pretty=1&attachments='. urlencode(json_encode([$attachments]));
56
        
57
        @file_get_contents($url);
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

57
        /** @scrutinizer ignore-unhandled */ @file_get_contents($url);

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...
58
    } // end send
59
    
60
}
61