Passed
Push — master ( fbe9b0...b04ac9 )
by Elf
02:17 queued 36s
created

BearyChatChannel   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 57.89%

Importance

Changes 7
Bugs 1 Features 0
Metric Value
eloc 20
dl 0
loc 56
ccs 11
cts 19
cp 0.5789
rs 10
c 7
b 1
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B send() 0 28 8
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 12
    public function __construct(ClientManager $clientManager)
27
    {
28 12
        $this->clientManager = $clientManager;
29 12
    }
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 8
    public function send($notifiable, Notification $notification)
41
    {
42 8
        $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 8
        if (! $message instanceof Message) {
45 4
            throw CouldNotSendNotification::invalidMessage($message);
46
        }
47
48 4
        if ($route = $notifiable->routeNotificationFor('BearyChat')) {
49
            if (Str::startsWith($route, ['@', '#'])) {
50
                $message->to($route);
51
            } elseif (Str::startsWith($route, ['http://', 'https://'])) {
52
                if ($client = $message->getClient()) {
53
                    $client->webhook($route);
54
                } else {
55
                    $message->setClient(new Client($route));
56
                }
57
            } else {
58
                $message->setClient($this->clientManager->client($route));
59
            }
60
        }
61
62 4
        if (is_null($message->getClient())) {
63
            $message->setClient($this->clientManager->client());
64
        }
65
66 4
        if (! $message->send()) {
67 4
            throw CouldNotSendNotification::sendingFailed($message->getClient()->getWebhook(), $message);
68
        }
69
    }
70
}
71