Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Vonage
0.00% covered (danger)
0.00%
0 / 37
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 / 24
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
2namespace BuyerKiosk\SMS;
3
4class Vonage
5{
6    private $key;
7    private $secret;
8    private $from;
9    private $log;
10    private $store;
11
12    public function __construct($store)
13    {
14        $this->key = $_ENV['VONAGE_KEY'];
15        $this->secret = $_ENV['VONAGE_SECRET'];
16        $this->from = $_ENV['VONAGE_NUMBER'];
17        $this->log = new \KLogger($_ENV['LOG_DIR'] . "/vonage.log", \KLogger::DEBUG);
18        $this->store = $store;
19    }
20
21    public function send($phone, $message)
22    {
23        if($this->checkPhoneForDoNotText($phone)) {
24            error_log("Phone number " . $phone . " is on the Do Not Text list");
25            return false;
26        }
27        $postfields = array(
28            'api_key' => $this->key,
29            'api_secret' => $this->secret,
30            'from' => $this->from,
31            'text' => $message,
32            'to' => "+1" . $phone
33        );
34        $ch = curl_init();
35        curl_setopt($ch, CURLOPT_URL, 'https://rest.nexmo.com/sms/json');
36        curl_setopt($ch, CURLOPT_POST, 1);
37        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
38        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
39        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
40        $result = curl_exec($ch);
41        $resultJSON = json_decode($result);
42        $errorFlag = 0;
43        foreach ($resultJSON->messages as $message) {
44            if ($message->status != 0) {
45                $errorFlag = 1;
46                $this->log->LogError("Resend SMS from Store[" . $this->store->getTypeNum() . "][" . $phone . "] with error: " . $message->status);
47            }
48        }
49        return !$errorFlag;
50    }
51    private function checkPhoneForDoNotText($phone)
52    {
53        $db = dbConnectByName($_ENV['DB_NAME']);
54        $sql = "SELECT COUNT(*) FROM loyaltyDoNotTextList WHERE phone = :phone";
55        $stmt = $db->prepare($sql);
56        $stmt->bindParam(":phone", $phone);
57        if ($stmt->execute()) {
58            $count = $stmt->fetchColumn();
59            return $count > 0;
60        }
61        return false;
62    }
63}