Completed
Push — master ( a92f2a...f6862d )
by Renan William Alves de
49s
created

HangoutsChatChannel::send()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.074

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 10
cts 12
cp 0.8333
rs 9.584
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 4.074
1
<?php
2
3
namespace NotificationChannels\GoogleHangoutsChat;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Notifications\Notification;
7
use Illuminate\Support\Arr;
8
use NotificationChannels\GoogleHangoutsChat\Exceptions\CouldNotSendNotification;
9
use Psr\Http\Message\ResponseInterface;
10
11
class HangoutsChatChannel
12
{
13
    /** @var Client */
14
    protected $client;
15
16
    /**
17
     * @param Client $client
18
     */
19 1
    public function __construct(Client $client)
20
    {
21 1
        $this->client = $client;
22 1
    }
23
24
    /**
25
     * Send the given notification.
26
     *
27
     * @param mixed $notifiable
28
     * @param Notification $notification
29
     *
30
     * @return ResponseInterface|void
31
     *
32
     * @throws CouldNotSendNotification
33
     */
34 1
    public function send($notifiable, Notification $notification)
35
    {
36 1
        if (! $url = $notifiable->routeNotificationFor('hangoutsChat')) {
37
            return;
38
        }
39
40 1
        $webhookData = $notification->{'toHangoutsChat'}($notifiable)->toArray();
41
42 1
        $response = $this->client->post($url, [
43 1
            'query' => Arr::get($webhookData, 'query'),
44 1
            'body' => json_encode(Arr::get($webhookData, 'data')),
45 1
            'verify' => Arr::get($webhookData, 'verify'),
46 1
            'headers' => Arr::get($webhookData, 'headers'),
47
        ]);
48
49 1
        if ($response->getStatusCode() >= 300 || $response->getStatusCode() < 200) {
50 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
51
        }
52
53
        return $response;
54
    }
55
}
56