|
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; |
|
|
|
|
|
|
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
|
|
|
|