Completed
Push — master ( 9b67ab...07e91d )
by Luke
04:55
created

PagerDutyChannel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1.0156
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 8
    public function __construct(Client $client)
17
    {
18 8
        $this->client = $client;
19 8
    }
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 8
    public function send($notifiable, Notification $notification)
30
    {
31 8
        if (! $routing_key = $notifiable->routeNotificationFor('PagerDuty')) {
32
            return;
33
        }
34
35
        /** @var PagerDutyMessage $data */
36 8
        $data = $notification->toPagerDuty($notifiable);
37 8
        $data->routingKey($routing_key);
38
39 8
        $response = $this->client->post('https://events.pagerduty.com/v2/enqueue', [
40 8
            'body' => json_encode($data->toArray()),
41 8
        ]);
42
43 8
        switch ($response->getStatusCode()) {
44 8
            case 200:
45 8
            case 201:
46 8
            case 202:
47 2
                return;
48 6
            case 400:
49 4
                throw CouldNotSendNotification::serviceBadRequest($response->getBody());
50 2
            case 429:
51 2
                throw CouldNotSendNotification::rateLimit();
52
            default:
53
                throw CouldNotSendNotification::unknownError($response->getStatusCode());
54
        }
55
    }
56
}
57