Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
38.89% |
7 / 18 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| TwilioSmsChannel | |
38.89% |
7 / 18 |
|
60.00% |
3 / 5 |
22.61 | |
0.00% |
0 / 1 |
| key | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| label | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| configSchema | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| send | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| isImplemented | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services\Notification\Channels; |
| 4 | |
| 5 | use App\Services\Notification\Contracts\NotificationChannelContract; |
| 6 | use Illuminate\Support\Facades\Http; |
| 7 | |
| 8 | /** Twilio Messages API — https://www.twilio.com/docs/sms/api/message-resource. */ |
| 9 | class TwilioSmsChannel implements NotificationChannelContract |
| 10 | { |
| 11 | public function key(): string |
| 12 | { |
| 13 | return 'twilio'; |
| 14 | } |
| 15 | |
| 16 | public function label(): string |
| 17 | { |
| 18 | return 'SMS — Twilio'; |
| 19 | } |
| 20 | |
| 21 | public function configSchema(): array |
| 22 | { |
| 23 | return [ |
| 24 | 'account_sid' => ['type' => 'text', 'label' => 'Account SID', 'required' => true], |
| 25 | 'auth_token' => ['type' => 'password', 'label' => 'Auth Token', 'required' => true], |
| 26 | 'from_number' => ['type' => 'text', 'label' => 'From number', 'required' => true, 'help' => 'e.g. +15017122661'], |
| 27 | ]; |
| 28 | } |
| 29 | |
| 30 | public function send(string $to, string $subject, string $body, array $config): array |
| 31 | { |
| 32 | if (empty($config['account_sid']) || empty($config['auth_token']) || empty($config['from_number'])) { |
| 33 | return ['ok' => false, 'response' => 'Twilio is not configured.']; |
| 34 | } |
| 35 | |
| 36 | $response = Http::asForm() |
| 37 | ->withBasicAuth($config['account_sid'], $config['auth_token']) |
| 38 | ->post("https://api.twilio.com/2010-04-01/Accounts/{$config['account_sid']}/Messages.json", [ |
| 39 | 'To' => $to, |
| 40 | 'From' => $config['from_number'], |
| 41 | 'Body' => $body, |
| 42 | ]); |
| 43 | |
| 44 | return ['ok' => $response->successful(), 'response' => $response->body()]; |
| 45 | } |
| 46 | |
| 47 | public function isImplemented(): bool |
| 48 | { |
| 49 | return true; |
| 50 | } |
| 51 | } |