Completed
Push — master ( 7cd9c2...0e7751 )
by dan
02:16
created

SlackWebhookMessageDispatcher::dispatch()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Dispatcher;
4
5
use IrishDan\NotificationBundle\Message\MessageInterface;
6
7
class SlackWebhookMessageDispatcher implements MessageDispatcherInterface
8
{
9
    public function dispatch(MessageInterface $message)
10
    {
11
        // Get the dispatch and message data from the message.
12
        $dispatchData = $message->getDispatchData();
13
        $messageData = $message->getMessageData();
14
15
        // Build payload from the message data.
16
        $messageData['text'] = $messageData['body'];
17
        unset($messageData['body']);
18
19
        $data = 'payload=' . json_encode($messageData);
20
21
        $ch = curl_init();
22
        curl_setopt($ch, CURLOPT_URL, $dispatchData['webhook']);
23
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
24
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
25
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
26
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
27
        $result = curl_exec($ch);
28
29
        if ($result !== 'ok') {
30
            return false;
31
        }
32
33
        curl_close($ch);
34
35
        return true;
36
    }
37
}