<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

namespace E4J\VikRestaurants\Cron\Traits;

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

/**
 * Helper trait used to implement common sending methods for SMS purposes.
 * 
 * @since 1.10
 */
trait NotificationSmsDispatcher
{
    /**
     * Sends the SMS notification to the customer of the provided reservation.
     * 
     * @param   string|array  $recipients
     * @param   string        $template
     * 
     * @return  void
     * 
     * @throws  \Exception
     */
    protected function sendNotification($recipients, string $template)
    {
        if (!$recipients)
        {
            // missing recipient
            throw new \InvalidArgumentException('Missing phone number.', 400);
        }

        // instantiate SMS API driver
        $smsapi = \VREApplication::getInstance()->getSmsInstance();

        foreach ((array) $recipients as $phone)
        {
            // send message
            $response = $smsapi->sendMessage($phone, $template);

            // validate response
            if (!$smsapi->validateResponse($response))
            {
                // unable to deliver the SMS message
                throw new \UnexpectedValueException($smsapi->getLog(), 500);
            }
        }
    }
}
