<?php

namespace App\Services\Sms;

class NexmoService implements SmsServiceInterface
{
    protected $client;

    public function __construct()
    {
        $basic = new \Vonage\Client\Credentials\Basic(config('sms.drivers.nexmo.api_key'), config('sms.drivers.nexmo.api_secret'));
        $this->client = new \Vonage\Client($basic);
    }

    public function send(string $to, string $message)
    {
        $response = $this->client->sms()->send(
            new \Vonage\SMS\Message\SMS($to, config('sms.drivers.nexmo.from'), $message)
        );

        $message = $response->current();

        if ($message->getStatus() == 0) {
            return true;
        } else {
            throw new \Exception('Failed to send SMS: '.$message->getStatus());
        }
    }
}
