Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| TwilioTextSender | |
0.00% |
0 / 23 |
|
0.00% |
0 / 2 |
12 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| sendText | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\SMS\TextMessageService; |
| 4 | |
| 5 | // Include the modern Twilio autoloader |
| 6 | require_once($_ENV['HOME_DIR'] . '/public_html/api/model/Twilio/autoload.php'); |
| 7 | |
| 8 | use Twilio\Rest\Client; |
| 9 | |
| 10 | class TwilioTextSender implements TextSenderInterface |
| 11 | { |
| 12 | protected $store; |
| 13 | protected $usageLog; |
| 14 | |
| 15 | public function __construct($store) |
| 16 | { |
| 17 | $this->store = $store; |
| 18 | $this->usageLog = new \KLogger($_ENV['LOG_DIR'] . "/twilio_usage.log", \KLogger::INFO); |
| 19 | } |
| 20 | |
| 21 | public function sendText(string $messageText, string $phone) |
| 22 | { |
| 23 | try { |
| 24 | $client = new Client($_ENV['TWILIO_SID'], $_ENV['TWILIO_TOKEN']); |
| 25 | |
| 26 | $message = $client->messages->create($phone, [ |
| 27 | 'from' => $this->store->getTwilioPhone(), |
| 28 | 'body' => $messageText |
| 29 | ]); |
| 30 | |
| 31 | // If Twilio doesn't throw an exception, we consider it success |
| 32 | $this->usageLog->LogInfo("TWILIO_SUCCESS,phone=" . $phone . ",message_id=" . $message->sid); |
| 33 | return [ |
| 34 | 'provider' => 'twilio', |
| 35 | 'status' => 'success', |
| 36 | 'id' => $message->sid, |
| 37 | 'error' => '' |
| 38 | ]; |
| 39 | } catch (\Exception $e) { |
| 40 | error_log("Twilio error: " . $e->getMessage()); |
| 41 | $this->usageLog->LogInfo("TWILIO_FAILED,phone=" . $phone . ",error=" . $e->getMessage()); |
| 42 | |
| 43 | return [ |
| 44 | 'provider' => 'twilio', |
| 45 | 'status' => 'failed', |
| 46 | 'id' => null, |
| 47 | 'error' => $e->getMessage() |
| 48 | ]; |
| 49 | } |
| 50 | } |
| 51 | } |