Completed
Push — master ( e57500...6b1ea1 )
by Michael
05:04
created

SlackClient::getPayload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
3
namespace ParityBit\DeploymentNotifier\Clients;
4
5
class SlackClient
6
{
7
    protected $webhookUrl;
8
    protected $channels = [];
9
    protected $iconUrl = null;
10
    protected $username = null;
11
12
    public function __construct($webhookUrl, $channels = [], $username = 'Deployment Notifier', $iconUrl = null)
13
    {
14
        $this->webhookUrl = $webhookUrl;
15
        $this->channels = $channels;
16
        $this->username = $username;
17
        $this->iconUrl = $iconUrl;
18
    }
19
20
    public function sendMessage($message)
21
    {
22
        foreach ($this->channels as $channel) {
23
            $payload = $this->getPayload($message, $channel);
24
25
            $ch = curl_init();
26
27
            curl_setopt($ch, \CURLOPT_URL, $this->webhookUrl);
28
29
            curl_setopt($ch, \CURLOPT_POST, 1);
30
            curl_setopt($ch, \CURLOPT_POSTFIELDS, 'payload=' .json_encode($payload));
31
32
            $result = curl_exec($ch);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
33
34
            curl_close($ch);
35
        }
36
    }
37
38
    protected function getPayload($message, $channel)
39
    {
40
        $payload = [];
41
42
        if (!is_null($this->iconUrl)) {
43
            $payload['icon_url'] = $this->iconUrl;
44
        }
45
46
        $payload['text'] = $message;
47
        $payload['channel'] = $channel;
48
        $payload['username'] = $this->username;
49
50
        return $payload;
51
    }
52
}
53