TwilioTexter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
ccs 6
cts 7
cp 0.8571
rs 9.9
cc 2
nc 2
nop 3
crap 2.0116
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Notifier\Channel\Sms;
6
7
use GuzzleHttp\ClientInterface;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\Exception\GuzzleException;
10
use GuzzleHttp\RequestOptions;
11
use Notifier\Exception\SendingMessageFailed;
12
13
final class TwilioTexter implements Texter
14
{
15
    public const API_BASE_URL = 'https://api.twilio.com';
16
17
    /** @var string */
18
    private $authId;
19
20
    /** @var string */
21
    private $authToken;
22
23
    /** @var ClientInterface */
24
    private $httpClient;
25
26 2
    public function __construct(string $authId, string $authToken, ClientInterface $httpClient = null)
27
    {
28 2
        $this->authId = $authId;
29 2
        $this->authToken = $authToken;
30
31 2
        if (null === $httpClient) {
32
            $httpClient = new Client();
33
        }
34
35 2
        $this->httpClient = $httpClient;
36 2
    }
37
38 2
    public function send(SmsMessage $message): void
39
    {
40
        try {
41 2
            $this->httpClient->post(
42 2
                self::API_BASE_URL . "/2010-04-01/Accounts/{$this->authId}/Messages.json",
43
                [
44 2
                    RequestOptions::AUTH => [$this->authId, $this->authToken],
45
                    RequestOptions::FORM_PARAMS => [
46 2
                        'Body' => $message->getText(),
47 2
                        'From' => $message->getFrom(),
48 2
                        'To' => $message->getTo(),
49
                    ],
50
                ]
51
            );
52 1
        } catch (GuzzleException $exception) {
53 1
            throw SendingMessageFailed::dueTo($exception);
54
        }
55 1
    }
56
}
57