Klasa do wysyłki smsow

<?php

namespace App\Utils\ExternalApi\SmsApiAdapter;

use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class SmsApiAdapter {

    /**
     * @var Container 
     */
    private $container = null;

    /**
     * @var string 
     */
    private $token = null;

    /**
     * @param Container $container
     */
    public function __construct(Container $container) {
        $this->container = $container;
    }

    /**
     * authorization
     * 
     * @return void
     */
    public function authorization(): void {
        $this->token = $this->container->getParameter('sms_smsapi_token');
    }

    /**
     * send sms
     * 
     * @param string $phoneNumber - phone number
     * @param string $senderName - sender name
     * @param string $content - message content
     * 
     * @return mixed
     */
    public function sendSms(string $phoneNumber, string $senderName, string $content) {
        $params = array(
            'to' => $phoneNumber,
            'from' => $senderName,
            'message' => "" . $content . "",
        );

        return $this->sms_send($params, $this->token);
    }

    /**
     * send sms 
     * 
     * @param array $params
     * @param string $token
     * @param bool $backup
     * @return mixes
     * 
     * @throws \Exception
     */
    private function sms_send(array $params, string $token, bool $backup = false) {
        $result = null;

        if ($backup == true) {
            $url = 'https://api2.smsapi.pl/sms.do';
        } else {
            $url = 'https://api.smsapi.pl/sms.do';
        }

        $c = curl_init();
        curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($c, CURLOPT_URL, $url);
        curl_setopt($c, CURLOPT_POST, true);
        curl_setopt($c, CURLOPT_POSTFIELDS, $params);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($c, CURLOPT_HTTPHEADER, array(
            "Authorization: Bearer $token"
        ));

        $result = curl_exec($c);
        $http_status = curl_getinfo($c, CURLINFO_HTTP_CODE);

        if ($http_status != 200 && $backup == false) {
            $backup = true;
            $this->sms_send($params, $token, $backup);
        }

        if (!(mb_strlen($result, 'utf-8') >= 2 && $result[0] == 'O' && $result[1] == 'K')) {
            throw new \Exception('SmsAPI - ' . $result);
        }

        curl_close($c);

        return $result;
    }

}
Komentarze wyłączone