Completed
Push — master ( 034402...fbe9b0 )
by Elf
03:33 queued 01:55
created

BearyChatChannel::send()   B

Complexity

Conditions 8
Paths 21

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8.0079

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 17
nc 21
nop 2
dl 0
loc 28
ccs 19
cts 20
cp 0.95
crap 8.0079
rs 8.4444
c 3
b 0
f 0
1
<?php
2
3
namespace NotificationChannels\BearyChat;
4
5
use ElfSundae\BearyChat\Client;
6
use ElfSundae\BearyChat\Laravel\ClientManager;
7
use ElfSundae\BearyChat\Message;
8
use Illuminate\Notifications\Notification;
9
use Illuminate\Support\Str;
10
use NotificationChannels\BearyChat\Exceptions\CouldNotSendNotification;
11
12
class BearyChatChannel
13
{
14
    /**
15
     * The BearyChat client manager.
16
     *
17
     * @var \ElfSundae\BearyChat\Laravel\ClientManager
18
     */
19
    protected $clientManager;
20
21
    /**
22
     * Create a new BearyChatChannel instance.
23
     *
24
     * @param  \ElfSundae\BearyChat\Laravel\ClientManager  $clientManager
25
     */
26 18
    public function __construct(ClientManager $clientManager)
27
    {
28 18
        $this->clientManager = $clientManager;
29 18
    }
30
31
    /**
32
     * Send the given notification.
33
     *
34
     * @param  mixed  $notifiable
35
     * @param  \Illuminate\Notifications\Notification  $notification
36
     * @return void
37
     *
38
     * @throws \NotificationChannels\BearyChat\Exceptions\CouldNotSendNotification
39
     */
40 14
    public function send($notifiable, Notification $notification)
41
    {
42 14
        $message = $notification->toBearyChat($notifiable);
0 ignored issues
show
introduced by
The method toBearyChat() does not exist on Illuminate\Notifications\Notification. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        /** @scrutinizer ignore-call */ 
43
        $message = $notification->toBearyChat($notifiable);
Loading history...
43
44 14
        if (! $message instanceof Message) {
45 4
            throw CouldNotSendNotification::invalidMessage($message);
46
        }
47
48 10
        if ($route = $notifiable->routeNotificationFor('BearyChat')) {
49 4
            if (Str::startsWith($route, ['@', '#'])) {
50 2
                $message->to($route);
51 4
            } elseif (Str::startsWith($route, ['http://', 'https://'])) {
52 1
                if ($client = $message->getClient()) {
53 1
                    $client->webhook($route);
54 1
                } else {
55
                    $message->setClient(new Client($route));
56
                }
57 1
            } else {
58 1
                $message->setClient($this->clientManager->client($route));
59
            }
60 4
        }
61
62 10
        if (is_null($message->getClient())) {
63 1
            $message->setClient($this->clientManager->client());
64 1
        }
65
66 10
        if (! $message->send()) {
67 4
            throw CouldNotSendNotification::sendingFailed($message->getClient()->getWebhook(), $message);
68
        }
69 6
    }
70
}
71