Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Twilio
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 send
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
 checkPhoneForDoNotText
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace BuyerKiosk\SMS;
4
5use function Laravel\Prompts\error;
6
7class Twilio
8{
9    private $sid;
10    private $token;
11    private $client;
12    private $store;
13    public function __construct($store)
14    {
15        $this->sid = $_ENV['TWILIO_SID'];
16        $this->token = $_ENV['TWILIO_TOKEN'];
17        $this->client = new \Twilio\Rest\Client($this->sid, $this->token);
18        $this->log = new \KLogger($_ENV['LOG_DIR']."/twilio.log", \KLogger::DEBUG);
19        // @var \Store $store
20        $this->store = $store;
21    }
22
23    public function send($phone, $message)
24    {
25        if($this->checkPhoneForDoNotText($phone)) {
26            error_log("Phone number " . $phone . " is on the Do Not Text list");
27            return false;
28        }
29        try {
30            $twilioPhone = $this->store->getTwilioPhone();
31            if(strlen($twilioPhone) < 10) {
32                $twilioPhone = "+19723474696";
33            }
34            $result = $this->client->messages->create(
35                '+1' . $phone,
36                array(
37                "from" => $twilioPhone,
38                "body" => $message,
39            ));
40        } catch (Services_Twilio_RestException $e) {
41            $this->log->LogError($e->getMessage());
42        }
43        return true;
44    }
45    private function checkPhoneForDoNotText($phone)
46    {
47        $db = dbConnectByName($_ENV['DB_NAME']);
48        $sql = "SELECT COUNT(*) FROM loyaltyDoNotTextList WHERE phone = :phone";
49        $stmt = $db->prepare($sql);
50        $stmt->bindParam(":phone", $phone);
51        if ($stmt->execute()) {
52            $count = $stmt->fetchColumn();
53            return $count > 0;
54        }
55        return false;
56    }
57}
58