bnomei/kirby3-mailjet

View on GitHub
classes/MailjetSMS.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

declare(strict_types=1);

namespace Bnomei;

use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

final class MailjetSMS
{
    /**
     * @var string
     */
    private $token;
    /**
     * @var MailjetLog
     */
    private $log;

    public function __construct(string $token, MailjetLog $log)
    {
        $this->token = $token;
        $this->log = $log;
    }

    /**
     * @param string $from
     * @param string $to
     * @param string $text
     * @return bool
     */
    public function send(string $from, string $to, string $text): bool
    {
        $request = new Request(
            'POST',
            'https://api.mailjet.com/v4/sms-send',
            [
                'Authorization' => 'Bearer ' . $this->token,
                'content-type' => 'application/json; charset=utf-8',
            ],
            json_encode([
                'From' => $from,
                'To' => $to,
                'Text' => $text
            ])
        );

        $response = null;
        try {
            $response = (new Client())->send($request);
        } catch (\Exception $exception) {
            return false;
        }

        return $response && $response->getStatusCode() === 200;
    }
}