SlackAdaptor   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 25
c 1
b 0
f 0
dl 0
loc 40
ccs 0
cts 17
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A send() 0 33 4
1
<?php
2
3
namespace App\Notifier;
4
5
use App\Facades\Log;
6
use App\Notifier\AdaptorInterface;
7
8
/**
9
 * Notification adaptor that sends messages to a slack webhook
10
 *
11
 * @author Ronan Chilvers <[email protected]>
12
 */
13
class SlackAdaptor extends AbstractAdaptor implements AdaptorInterface
14
{
15
    /**
16
     * {@inheridoc}
17
     *
18
     * @author Ronan Chilvers <[email protected]>
19
     */
20
    public function send(Notification $notification, array $options = [])
21
    {
22
        if (!isset($options['webhook'])) {
23
            Log::error('Invalid notification configuration - slack webhook missing', [
24
                'options' => $options,
25
            ]);
26
            return false;
27
        }
28
        if (false === ($curl = curl_init($options['webhook']))) {
29
            Log::error('Unable to initialise slack webhook curl handle');
30
            return false;
31
        }
32
        $payload = json_encode([
33
            'text' => $notification->getMessage(),
34
        ]);
35
        curl_setopt_array($curl, [
36
            CURLOPT_USERAGENT      => 'ronanchilvers/deploy - curl ' . curl_version()['version'],
37
            CURLOPT_FOLLOWLOCATION => false,
38
            CURLOPT_RETURNTRANSFER => true,
39
            CURLOPT_TIMEOUT        => 5,
40
            CURLOPT_POST           => true,
41
            CURLOPT_POSTFIELDS     => ['payload' => $payload],
42
        ]);
43
        $result = curl_exec($curl);
44
        $info = curl_getinfo($curl);
45
        if (!$result) {
46
            Log::error('Unable to send slack message to webhook', $info);
47
        } else {
48
            Log::debug('Sent slack message to webhook', $info);
49
        }
50
        curl_close($curl);
51
52
        return true;
53
    }
54
}
55