TwilioSmsHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 2
cbo 2
dl 0
loc 52
ccs 12
cts 25
cp 0.48
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setDefaultFrom() 0 4 1
A lookupNumber() 0 10 2
A sendSMS() 0 16 3
1
<?php
2
/**
3
 * This file is part of the Apiary SMS Login Provider package.
4
 *
5
 * (c) Darren Mothersele <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Apiary\SmsLoginProvider\SmsHandler;
0 ignored issues
show
Coding Style introduced by
There must be one blank line after the namespace declaration
Loading history...
11
12
13
use Apiary\SmsLoginProvider\Exception\InvalidPhoneNumberException;
14
use Apiary\SmsLoginProvider\Exception\SmsSendFailException;
15
use Lookups_Services_Twilio;
16
use Services_Twilio_RestException;
17
use Services_Twilio;
18
19
class TwilioSmsHandler implements SmsHandlerInterface
20
{
21
22
    private $lookupClient;
23
    private $client;
24
    private $from = '';
25
26
    /**
27
     * TwilioSMSHandler constructor.
28
     * @param Lookups_Services_Twilio $lookupClient
29
     * @param Services_Twilio $client
30
     */
31 9
    public function __construct(Lookups_Services_Twilio $lookupClient, Services_Twilio $client)
32
    {
33 9
        $this->client = $client;
34 9
        $this->lookupClient = $lookupClient;
35 9
    }
36
37 3
    public function setDefaultFrom($from)
38
    {
39 3
        $this->from = $from;
40 3
    }
41
42
43 3
    public function lookupNumber($number, $countryCode)
44
    {
45
        try {
46 3
            $number = $this->lookupClient->phone_numbers->get($number,
47 3
                ['CountryCode' => $countryCode]);
48 3
        } catch (Services_Twilio_RestException $e) {
49
            throw new InvalidPhoneNumberException($e->getMessage());
50
        }
51 3
        return $number->phone_number;
52
    }
53
54
    public function sendSMS($to, $body, $from = null)
55
    {
56
        if ($from === null) {
57
            $from = $this->from;
58
        }
59
        try {
60
            $sms = $this->client->account->messages->create([
61
                'From' => $from,
62
                'To' => $to,
63
                'Body' => $body,
64
            ]);
65
        } catch (Services_Twilio_RestException $e) {
66
            throw new SMSSendFailException($e->getMessage());
67
        }
68
        return $sms->sid;
69
    }
70
}
71