SlackClient::sendMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace ParityBit\DeploymentNotifier\Clients;
4
5
use Curl\Curl;
6
7
class SlackClient
8
{
9
    protected $curl;
10
    protected $webhookUrl;
11
    protected $channels = [];
12
    protected $iconUrl = null;
13
    protected $username = null;
14
15
    public function __construct(Curl $curl, $webhookUrl, $channels = [], $username = 'Deployment Notifier', $iconUrl = null)
16
    {
17
        $this->curl = $curl;
18
        $this->webhookUrl = $webhookUrl;
19
        $this->channels = $channels;
20
        $this->username = $username;
21
        $this->iconUrl = $iconUrl;
22
    }
23
24
    public function sendMessage($message)
25
    {
26
        foreach ($this->channels as $channel) {
27
            $payload = $this->getPayload($message, $channel);
28
29
            $this->curl->post(
30
                $this->webhookUrl,
31
                'payload=' . json_encode($payload)
0 ignored issues
show
Documentation introduced by
'payload=' . json_encode($payload) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32
            );
33
        }
34
    }
35
36
    public function getPayload($message, $channel)
37
    {
38
        $payload = [];
39
40
        if (!is_null($this->iconUrl)) {
41
            $payload['icon_url'] = $this->iconUrl;
42
        }
43
44
        $payload['text'] = $message;
45
        $payload['channel'] = $channel;
46
        $payload['username'] = $this->username;
47
48
        return $payload;
49
    }
50
}
51