Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
36.84% |
7 / 19 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| VonageSmsChannel | |
36.84% |
7 / 19 |
|
60.00% |
3 / 5 |
24.12 | |
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 / 11 |
|
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 | /** Vonage (formerly Nexmo) SMS API — https://developer.vonage.com/en/messaging/sms/overview. */ |
| 9 | class VonageSmsChannel implements NotificationChannelContract |
| 10 | { |
| 11 | public function key(): string |
| 12 | { |
| 13 | return 'vonage'; |
| 14 | } |
| 15 | |
| 16 | public function label(): string |
| 17 | { |
| 18 | return 'SMS — Vonage'; |
| 19 | } |
| 20 | |
| 21 | public function configSchema(): array |
| 22 | { |
| 23 | return [ |
| 24 | 'api_key' => ['type' => 'text', 'label' => 'API Key', 'required' => true], |
| 25 | 'api_secret' => ['type' => 'password', 'label' => 'API Secret', 'required' => true], |
| 26 | 'from_name' => ['type' => 'text', 'label' => 'Sender ID', 'required' => true, 'help' => 'Brand name or number shown as the sender.'], |
| 27 | ]; |
| 28 | } |
| 29 | |
| 30 | public function send(string $to, string $subject, string $body, array $config): array |
| 31 | { |
| 32 | if (empty($config['api_key']) || empty($config['api_secret'])) { |
| 33 | return ['ok' => false, 'response' => 'Vonage is not configured.']; |
| 34 | } |
| 35 | |
| 36 | $response = Http::asForm()->post('https://rest.nexmo.com/sms/json', [ |
| 37 | 'api_key' => $config['api_key'], |
| 38 | 'api_secret' => $config['api_secret'], |
| 39 | 'to' => $to, |
| 40 | 'from' => $config['from_name'] ?? 'Store', |
| 41 | 'text' => $body, |
| 42 | ]); |
| 43 | |
| 44 | $status = data_get($response->json(), 'messages.0.status'); |
| 45 | |
| 46 | return ['ok' => $response->successful() && $status === '0', 'response' => $response->body()]; |
| 47 | } |
| 48 | |
| 49 | public function isImplemented(): bool |
| 50 | { |
| 51 | return true; |
| 52 | } |
| 53 | } |