1 | <?php |
||
2 | |||
3 | namespace NotificationChannels\AllMySms; |
||
4 | |||
5 | use Illuminate\Notifications\Notification; |
||
6 | use NotificationChannels\AllMySms\Exceptions\CouldNotSendNotification; |
||
7 | |||
8 | class AllMySmsChannel |
||
9 | { |
||
10 | /** @var \NotificationChannels\AllMySms\AllMySms */ |
||
11 | protected $client; |
||
12 | |||
13 | /** |
||
14 | * The sender name the message should sent from. |
||
15 | * |
||
16 | * @var string|null |
||
17 | */ |
||
18 | public $sender; |
||
19 | |||
20 | /** |
||
21 | * The phone number the message should always send to. |
||
22 | * |
||
23 | * @var string|null |
||
24 | */ |
||
25 | protected $to; |
||
26 | |||
27 | /** |
||
28 | * Create a new AllMySmsChannel instance. |
||
29 | * |
||
30 | * @param \NotificationChannels\AllMySms\AllMySms $client |
||
31 | * @param string|null $sender |
||
32 | * @param string|null $to |
||
33 | */ |
||
34 | 2 | public function __construct(AllMySms $client, ?string $sender = null, ?string $to = null) |
|
35 | { |
||
36 | 2 | $this->client = $client; |
|
37 | 2 | $this->sender = $sender; |
|
38 | 2 | $this->to = $to; |
|
39 | 2 | } |
|
40 | |||
41 | /** |
||
42 | * Send the given notification. |
||
43 | * |
||
44 | * @param mixed $notifiable |
||
45 | * @param \Illuminate\Notifications\Notification $notification |
||
46 | * @return void |
||
47 | * |
||
48 | * @throws \NotificationChannels\AllMySms\Exceptions\CouldNotSendNotification |
||
49 | */ |
||
50 | 2 | public function send($notifiable, Notification $notification) |
|
51 | { |
||
52 | 2 | if (! $to = $notifiable->routeNotificationFor('AllMySms', $notification)) { |
|
53 | return; |
||
54 | } |
||
55 | |||
56 | 2 | if (! empty($this->to)) { |
|
57 | $to = $this->to; |
||
58 | } |
||
59 | |||
60 | 2 | $message = $notification->toAllMySms($notifiable); |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
61 | |||
62 | 2 | if (is_string($message)) { |
|
63 | 2 | $message = new AllMySmsMessage($message); |
|
64 | } |
||
65 | |||
66 | 2 | $response = $this->client->sendSms($to, $message->toArray(), $this->sender); |
|
67 | |||
68 | 2 | if ($response->getStatusCode() !== 200) { |
|
69 | 2 | throw CouldNotSendNotification::serviceRespondedWithAnError($response); |
|
70 | } |
||
71 | } |
||
72 | } |
||
73 |