Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 130 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| TextMessageService | |
0.00% |
0 / 130 |
|
0.00% |
0 / 9 |
1332 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| sendServiceText | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
30 | |||
| sendBuyText | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
30 | |||
| sendSurveyText | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
30 | |||
| sendCustomText | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
30 | |||
| buildServiceText | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| buildBuyText | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| buildSurveyText | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| isOnDoNotTextList | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\SMS\TextMessageService; |
| 4 | |
| 5 | class TextMessageService |
| 6 | { |
| 7 | protected $store; |
| 8 | protected $storeDB; |
| 9 | protected $log; |
| 10 | protected $textSender; |
| 11 | protected $floodProtector; |
| 12 | |
| 13 | public function __construct(TextSenderInterface $textSender, $store, $storeDB) |
| 14 | { |
| 15 | $this->textSender = $textSender; |
| 16 | $this->store = $store; |
| 17 | $this->storeDB = $storeDB; |
| 18 | $this->floodProtector = new FloodProtector($store); |
| 19 | $this->log = \KLoggerFactory::getLogger($_ENV['LOG_DIR']."/sms.log", \KLogger::DEBUG); |
| 20 | } |
| 21 | |
| 22 | public function sendServiceText($customer) |
| 23 | { |
| 24 | if (!preg_match('/^[0-9]{10}$/', $customer->phone)) { |
| 25 | return [ |
| 26 | 'provider' => 'validator', |
| 27 | 'status' => 'failed', |
| 28 | 'id' => null, |
| 29 | 'error' => "Invalid phone number: {$customer->phone}" |
| 30 | ]; |
| 31 | } |
| 32 | |
| 33 | if ($this->isOnDoNotTextList($customer->phone)) { |
| 34 | $this->log->LogDebug("Phone {$customer->phone} is on do-not-text list"); |
| 35 | return [ |
| 36 | 'provider' => 'validator', |
| 37 | 'status' => 'failed', |
| 38 | 'id' => null, |
| 39 | 'error' => "Phone number is on do-not-text list" |
| 40 | ]; |
| 41 | } |
| 42 | |
| 43 | $floodCheck = $this->floodProtector->canSendMessage($customer->phone); |
| 44 | if ($floodCheck['status'] === 'failed') { |
| 45 | return $floodCheck; |
| 46 | } |
| 47 | |
| 48 | $messageText = $this->buildServiceText($customer); |
| 49 | $result = $this->textSender->sendText($messageText, $customer->phone); |
| 50 | |
| 51 | if ($result['status'] === 'success') { |
| 52 | $this->floodProtector->logMessage($customer->phone); |
| 53 | } |
| 54 | |
| 55 | return $result; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | public function sendBuyText($customer) |
| 60 | { |
| 61 | if (!preg_match('/^[0-9]{10}$/', $customer->phone)) { |
| 62 | return [ |
| 63 | 'provider' => 'validator', |
| 64 | 'status' => 'failed', |
| 65 | 'id' => null, |
| 66 | 'error' => "Invalid phone number: {$customer->phone}" |
| 67 | ]; |
| 68 | } |
| 69 | |
| 70 | if ($this->isOnDoNotTextList($customer->phone)) { |
| 71 | $this->log->LogDebug("Phone {$customer->phone} is on do-not-text list"); |
| 72 | return [ |
| 73 | 'provider' => 'validator', |
| 74 | 'status' => 'failed', |
| 75 | 'id' => null, |
| 76 | 'error' => "Phone number is on do-not-text list" |
| 77 | ]; |
| 78 | } |
| 79 | |
| 80 | $floodCheck = $this->floodProtector->canSendMessage($customer->phone); |
| 81 | if ($floodCheck['status'] === 'failed') { |
| 82 | return $floodCheck; |
| 83 | } |
| 84 | |
| 85 | $messageText = $this->buildBuyText($customer); |
| 86 | $result = $this->textSender->sendText($messageText, $customer->phone); |
| 87 | |
| 88 | if ($result['status'] === 'success') { |
| 89 | $this->floodProtector->logMessage($customer->phone); |
| 90 | } |
| 91 | |
| 92 | return $result; |
| 93 | } |
| 94 | |
| 95 | public function sendSurveyText($customer, $surveyUrl) |
| 96 | { |
| 97 | if (!preg_match('/^[0-9]{10}$/', $customer->phone)) { |
| 98 | return [ |
| 99 | 'provider' => 'validator', |
| 100 | 'status' => 'failed', |
| 101 | 'id' => null, |
| 102 | 'error' => "Invalid phone number: {$customer->phone}" |
| 103 | ]; |
| 104 | } |
| 105 | |
| 106 | if ($this->isOnDoNotTextList($customer->phone)) { |
| 107 | $this->log->LogDebug("Phone {$customer->phone} is on do-not-text list"); |
| 108 | return [ |
| 109 | 'provider' => 'validator', |
| 110 | 'status' => 'failed', |
| 111 | 'id' => null, |
| 112 | 'error' => "Phone number is on do-not-text list" |
| 113 | ]; |
| 114 | } |
| 115 | |
| 116 | $floodCheck = $this->floodProtector->canSendMessage($customer->phone); |
| 117 | if ($floodCheck['status'] === 'failed') { |
| 118 | return $floodCheck; |
| 119 | } |
| 120 | |
| 121 | $messageText = $this->buildSurveyText($customer, $surveyUrl); |
| 122 | $result = $this->textSender->sendText($messageText, $customer->phone); |
| 123 | |
| 124 | if ($result['status'] === 'success') { |
| 125 | $this->floodProtector->logMessage($customer->phone); |
| 126 | } |
| 127 | |
| 128 | return $result; |
| 129 | } |
| 130 | |
| 131 | public function sendCustomText($customer, $message) |
| 132 | { |
| 133 | if (!preg_match('/^[0-9]{10}$/', $customer->phone)) { |
| 134 | return [ |
| 135 | 'provider' => 'validator', |
| 136 | 'status' => 'failed', |
| 137 | 'id' => null, |
| 138 | 'error' => "Invalid phone number: {$customer->phone}" |
| 139 | ]; |
| 140 | } |
| 141 | |
| 142 | if ($this->isOnDoNotTextList($customer->phone)) { |
| 143 | $this->log->LogDebug("Phone {$customer->phone} is on do-not-text list"); |
| 144 | return [ |
| 145 | 'provider' => 'validator', |
| 146 | 'status' => 'failed', |
| 147 | 'id' => null, |
| 148 | 'error' => "Phone number is on do-not-text list" |
| 149 | ]; |
| 150 | } |
| 151 | |
| 152 | $floodCheck = $this->floodProtector->canSendMessage($customer->phone); |
| 153 | if ($floodCheck['status'] === 'failed') { |
| 154 | return $floodCheck; |
| 155 | } |
| 156 | |
| 157 | $result = $this->textSender->sendText($message, $customer->phone); |
| 158 | |
| 159 | if ($result['status'] === 'success') { |
| 160 | $this->floodProtector->logMessage($customer->phone); |
| 161 | } |
| 162 | |
| 163 | return $result; |
| 164 | } |
| 165 | |
| 166 | private function buildServiceText($customer) |
| 167 | { |
| 168 | $name = getStoreType($this->store); |
| 169 | $firstName = isset($customer->firstName) ? $customer->firstName : ''; |
| 170 | $storePhone = $this->store->getPhone(); |
| 171 | |
| 172 | $message = ''; |
| 173 | |
| 174 | // Handle first name if valid |
| 175 | if (mb_strlen($firstName) <= 20 && $firstName !== '') { |
| 176 | $message = "{$firstName}, your equipment service at {$name} is ready for pickup!"; |
| 177 | } else { |
| 178 | $message = "Your equipment service at {$name} is ready for pickup!"; |
| 179 | } |
| 180 | |
| 181 | // Base message with opt out |
| 182 | $message .= " Automated Message. Reply STOP to opt out."; |
| 183 | |
| 184 | // Only append phone if it exists and isn't empty |
| 185 | if (!empty(trim($storePhone))) { |
| 186 | $message .= " Store phone: " . $storePhone; |
| 187 | } |
| 188 | |
| 189 | return $message; |
| 190 | } |
| 191 | |
| 192 | private function buildBuyText($customer) |
| 193 | { |
| 194 | $name = getStoreType($this->store); |
| 195 | $firstName = isset($customer->firstName) ? $customer->firstName : ''; |
| 196 | $storePhone = $this->store->getPhone(); |
| 197 | |
| 198 | $message = ''; |
| 199 | |
| 200 | // Handle first name if valid |
| 201 | if (mb_strlen($firstName) <= 20 && $firstName !== '') { |
| 202 | $message = "{$firstName}, your offer at {$name} is ready for pickup!"; |
| 203 | } else { |
| 204 | $message = "Your offer at {$name} is ready for pickup!"; |
| 205 | } |
| 206 | |
| 207 | // Base message with opt out |
| 208 | $message .= " Automated Message. Reply STOP to opt out."; |
| 209 | |
| 210 | // Only append phone if it exists and isn't empty |
| 211 | if (!empty(trim($storePhone))) { |
| 212 | $message .= " Store phone: " . $storePhone; |
| 213 | } |
| 214 | |
| 215 | return $message; |
| 216 | } |
| 217 | |
| 218 | private function buildSurveyText($customer, $surveyUrl) |
| 219 | { |
| 220 | // Use the store's survey SMS text if available, otherwise use a default |
| 221 | $surveyText = $this->store->getSurveySMSText(); |
| 222 | if (empty($surveyText)) { |
| 223 | $surveyText = "Thank you for visiting us today!. Please take a moment to rate your experience: "; |
| 224 | } |
| 225 | |
| 226 | return $surveyText . $surveyUrl; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Check if a phone number is on the do-not-text list |
| 231 | * @param string $phone The 10-digit phone number to check |
| 232 | * @return bool True if the phone is on the do-not-text list |
| 233 | */ |
| 234 | private function isOnDoNotTextList(string $phone): bool |
| 235 | { |
| 236 | try { |
| 237 | $db = dbConnectByName($_ENV['DB_NAME']); |
| 238 | $stmt = $db->prepare("SELECT COUNT(*) FROM loyaltyDoNotTextList WHERE phone = :phone"); |
| 239 | $stmt->bindParam(":phone", $phone); |
| 240 | if ($stmt->execute()) { |
| 241 | return $stmt->fetchColumn() > 0; |
| 242 | } |
| 243 | } catch (\PDOException $e) { |
| 244 | $this->log->LogError("Error checking do-not-text list: " . $e->getMessage()); |
| 245 | } |
| 246 | return false; |
| 247 | } |
| 248 | } |