Completed
Push — master ( c2e735...654031 )
by Luke
05:44
created

PagerDutyChannel::send()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7.1086

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 27
ccs 20
cts 23
cp 0.8696
rs 6.7272
c 1
b 0
f 0
cc 7
eloc 18
nc 7
nop 2
crap 7.1086
1
<?php
2
3
namespace NotificationChannels\PagerDuty;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Notifications\Notification;
7
use NotificationChannels\PagerDuty\Exceptions\CouldNotSendNotification;
8
9
class PagerDutyChannel
10
{
11
    /**
12
     * @var Client
13
     */
14
    protected $client;
15
16 12
    public function __construct(Client $client)
17
    {
18 12
        $this->client = $client;
19 12
    }
20
21
    /**
22
     * Send the given notification.
23
     *
24
     * @param mixed $notifiable
25
     * @param \Illuminate\Notifications\Notification $notification
26
     *
27
     * @throws \NotificationChannels\PagerDuty\Exceptions\CouldNotSendNotification
28
     */
29 12
    public function send($notifiable, Notification $notification)
30
    {
31 12
        if (! $routing_key = $notifiable->routeNotificationFor('PagerDuty')) {
32 2
            return;
33
        }
34
35
        /** @var PagerDutyMessage $data */
36 10
        $data = $notification->toPagerDuty($notifiable);
37 10
        $data->routingKey($routing_key);
38
39 10
        $response = $this->client->post('https://events.pagerduty.com/v2/enqueue', [
40 10
            'body' => json_encode($data->toArray()),
41 10
        ]);
42
43 10
        switch ($response->getStatusCode()) {
44 10
            case 200:
45 10
            case 201:
46 10
            case 202:
47 2
                return;
48 8
            case 400:
49 4
                throw CouldNotSendNotification::serviceBadRequest($response->getBody());
50 4
            case 429:
51 2
                throw CouldNotSendNotification::rateLimit();
52 2
            default:
53 2
                throw CouldNotSendNotification::unknownError($response->getStatusCode());
54 2
        }
55
    }
56
}
57