Campfire::notify()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 16

Duplication

Lines 3
Ratio 10.71 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
loc 28
rs 8.5806
cc 4
eloc 16
nc 8
nop 3
1
<?php
2
3
namespace Behatch\Notifier;
4
5
class Campfire extends Notifier
6
{
7
    static private $url;
8
    static private $token;
9
    static private $room;
10
    static private $prefix;
11
    static private $icons = [];
12
13
    public function __construct($url, $token, $room, $prefix = null, $spamTimeout = 60, $icons = [])
14
    {
15
        parent::__construct($spamTimeout);
16
17
        self::$url = $url;
18
        self::$token = $token;
19
        self::$room = $room;
20
        self::$prefix = $prefix;
21
22
        self::$icons = $icons + [
23
            'sad' => ':thumbsdown:',
24
            'smile' => ':thumbsup::sparkles:',
25
            'error' => ':thumbsdown::shit:',
26
        ];
27
    }
28
29
    protected static function notify($status, $title, $message)
30
    {
31 View Code Duplication
        if (self::$prefix !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
32
            $title = '[' . self::$prefix . '] ' . $title;
33
        }
34
35
        if (isset(self::$icons[$status])) {
36
            $message .= ' ' . self::$icons[$status];
37
        }
38
39
        $body = "$title\n$message";
40
41
        $cmd = sprintf(
42
            "curl -s -u %s:X -H 'Content-Type: application/json' -d %s %s/room/%s/speak.json",
43
            self::$token,
44
            escapeshellarg(json_encode(['message' => ['body' => $body]])),
45
            trim(self::$url, '/'),
46
            self::$room
47
        );
48
49
        exec($cmd, $output, $return);
50
51
        if ($return !== 0) {
52
            throw new \Exception(
53
                sprintf("Unable to send campfire notification with curl :\n%s", implode("\n", $output))
54
            );
55
        }
56
    }
57
}
58