|
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); |
|
|
|
|
|
|
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
|
|
|
|