Completed
Push — master ( 570f1a...576848 )
by Nikola
03:44
created

TwilioTexter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
dl 0
loc 44
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2
ccs 14
cts 17
cp 0.8235
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A send() 0 18 2
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\Channel\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 1
    public function __construct(string $authId, string $authToken, ClientInterface $httpClient = null)
27
    {
28 1
        $this->authId = $authId;
29 1
        $this->authToken = $authToken;
30
31 1
        if (null === $httpClient) {
32
            $httpClient = new Client();
33
        }
34
35 1
        $this->httpClient = $httpClient;
36 1
    }
37
38 1
    public function send(SmsMessage $message): void
39
    {
40
        try {
41 1
            $this->httpClient->post(
42 1
                self::API_BASE_URL . "/2010-04-01/Accounts/{$this->authId}/Messages.json",
43
                [
44 1
                    RequestOptions::AUTH => [$this->authId, $this->authToken],
45
                    RequestOptions::FORM_PARAMS => [
46 1
                        'Body' => $message->text,
47 1
                        'From' => $message->from,
48 1
                        'To' => $message->to,
49
                    ],
50
                ]
51
            );
52
        } catch (GuzzleException $exception) {
53
            throw SendingMessageFailed::dueTo($exception);
54
        }
55 1
    }
56
}
57