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

SlackWebhookMessageDispatcher   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B dispatch() 0 28 2
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
}