1 | <?php |
||
8 | class Twilio |
||
9 | { |
||
10 | /** |
||
11 | * @var TwilioService |
||
12 | */ |
||
13 | protected $twilioService; |
||
14 | |||
15 | /** |
||
16 | * @var TwilioConfig |
||
17 | */ |
||
18 | private $config; |
||
19 | |||
20 | /** |
||
21 | * Twilio constructor. |
||
22 | * |
||
23 | * @param TwilioService $twilioService |
||
24 | * @param TwilioConfig $config |
||
25 | */ |
||
26 | 11 | public function __construct(TwilioService $twilioService, TwilioConfig $config) |
|
27 | { |
||
28 | 11 | $this->twilioService = $twilioService; |
|
29 | 11 | $this->config = $config; |
|
30 | 11 | } |
|
31 | |||
32 | /** |
||
33 | * Send a TwilioMessage to the a phone number. |
||
34 | * |
||
35 | * @param TwilioMessage $message |
||
36 | * @param string $to |
||
37 | * @param bool $useAlphanumericSender |
||
38 | * @return mixed |
||
39 | * @throws CouldNotSendNotification |
||
40 | */ |
||
41 | 10 | public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender = false) |
|
57 | |||
58 | /** |
||
59 | * Send an sms message using the Twilio Service. |
||
60 | * |
||
61 | * @param TwilioSmsMessage $message |
||
62 | * @param string $to |
||
63 | * @return \Twilio\Rest\Api\V2010\Account\MessageInstance |
||
64 | */ |
||
65 | 7 | protected function sendSmsMessage(TwilioSmsMessage $message, $to) |
|
66 | { |
||
67 | $params = [ |
||
68 | 7 | 'from' => $this->getFrom($message), |
|
69 | 6 | 'body' => trim($message->content), |
|
70 | ]; |
||
71 | |||
72 | 6 | if ($serviceSid = $this->config->getServiceSid()) { |
|
73 | 2 | $params['messagingServiceSid'] = $serviceSid; |
|
74 | } |
||
75 | |||
76 | 6 | return $this->twilioService->messages->create($to, $params); |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * Make a call using the Twilio Service. |
||
81 | * |
||
82 | * @param TwilioCallMessage $message |
||
83 | * @param string $to |
||
84 | * @return \Twilio\Rest\Api\V2010\Account\CallInstance |
||
85 | */ |
||
86 | 2 | protected function makeCall(TwilioCallMessage $message, $to) |
|
87 | { |
||
88 | 2 | return $this->twilioService->calls->create( |
|
89 | $to, |
||
90 | 2 | $this->getFrom($message), |
|
91 | 2 | ['url' => trim($message->content)] |
|
92 | ); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Get the from address from message, or config. |
||
97 | * |
||
98 | * @param TwilioMessage $message |
||
99 | * @return string |
||
100 | * @throws CouldNotSendNotification |
||
101 | */ |
||
102 | 9 | protected function getFrom(TwilioMessage $message) |
|
110 | |||
111 | /** |
||
112 | * Get the alphanumeric sender from config, if one exists. |
||
113 | * |
||
114 | * @return string|null |
||
115 | */ |
||
116 | 2 | protected function getAlphanumericSender() |
|
122 | } |
||
123 |