Completed
Push — master ( e2547a...b91078 )
by Tyler
04:34
created

Twilio::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace LeadThread\Sms\Drivers;
4
5
use Exception;
6
use Twilio\Rest\Client as Service;
7
use LeadThread\Sms\Drivers\Driver;
8
use LeadThread\Sms\Exceptions\InvalidPhoneNumberException;
9
use LeadThread\Sms\Interfaces\PhoneSearchParams;
10
use LeadThread\Sms\Responses\Twilio as TwilioResponse;
11
12
class Twilio extends Driver
13
{
14
    protected $handle;
15
    protected $auth_id;
16
17 12
    public function __construct($auth_id, $auth_token)
18
    {
19 12
        $this->auth_id = $auth_id;
20 12
        $this->handle = new Service($auth_id, $auth_token);
21 12
    }
22
23
    public function send($msg, $to, $from, $callback = null)
24
    {
25
        if (!empty($callback)) {
26
            throw new \Exception("Callback URLs are not implemented for Twilio", 1);
27
        }
28
        
29
        return new TwilioResponse($this->handle->account->messages->sendMessage($from, $to, $msg));
0 ignored issues
show
Bug introduced by
The property messages does not seem to exist. Did you mean _messages?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
30
    }
31
32
    public function searchNumber(PhoneSearchParams $search)
33
    {
34
        $resp = $this->handle->account->available_phone_numbers
35
            ->getList($search->getCountry(), 'Local', $search->toArray());
36
37
        return new TwilioResponse($resp);
38
    }
39
40
    public function buyNumber($phone)
41
    {
42
        $resp = $this->handle->account->incoming_phone_numbers->create(array(
43
            "PhoneNumber" => $phone,
44
        ));
45
46
        return new TwilioResponse($resp);
47
    }
48
49
    public function sellNumber($phone)
50
    {
51
        $sid = false;
52
53
        foreach ($this->handle->account->incoming_phone_numbers as $number) {
54
            if ($phone == $number->phone_number) {
55
                $sid = $number->sid;
56
            }
57
        }
58
        
59
        if (empty($sid)) {
60
            throw new InvalidPhoneNumberException("The phone number '{$phone}' could not be found on your account!");
61
        }
62
63
        $resp = $this->handle->account->incoming_phone_numbers->delete($sid);
64
        return new TwilioResponse($resp);
65
    }
66
}
67