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

PagerDutyChannel   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 70.37%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 48
ccs 19
cts 27
cp 0.7037
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C send() 0 27 7
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