Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 17 | class SMS |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Version number of the SMS API. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | const VERSION = '2.0.0'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Base URL. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | const BASE_URL = 'http://197.248.4.47/smsapi/'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Send SMS endpoint. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const SMS_ENDPOINT = 'submit.php'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Get Balance endpoint. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | const GET_BALANCE_ENDPOINT = 'balance.php'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * token is generated by md5(password). |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $token; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * timestamp is the current datetime. |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $timestamp; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * type of SMS to sent. |
||
| 63 | * |
||
| 64 | * @var int message |
||
| 65 | */ |
||
| 66 | protected $message_type = 0; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * batch type of the SMS being sent. |
||
| 70 | * |
||
| 71 | * @var int batch |
||
| 72 | */ |
||
| 73 | protected $batch_type; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * sms configurations. |
||
| 77 | * |
||
| 78 | * @array config |
||
| 79 | */ |
||
| 80 | protected $config; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * end point url. |
||
| 84 | * |
||
| 85 | * @string endpoint |
||
| 86 | */ |
||
| 87 | protected $endpoint; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * the message(s) being sent (array of messages in case message is different for each user. |
||
| 91 | * |
||
| 92 | * @var array. |
||
| 93 | */ |
||
| 94 | protected $message; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * the recipients . |
||
| 98 | * |
||
| 99 | * @var array. |
||
| 100 | */ |
||
| 101 | protected $recipients; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * SMS constructor. |
||
| 105 | */ |
||
| 106 | public function __construct() |
||
| 107 | { |
||
| 108 | $this->config = Config::get(); |
||
| 109 | |||
| 110 | if ( |
||
| 111 | empty($this->config['user_id']) || |
||
| 112 | empty($this->config['password']) || |
||
| 113 | empty($this->config['sender_id']) |
||
| 114 | ) { |
||
| 115 | throw new BongaTechException('Please ensure that all configuration variables have been set.'); |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->setTimestamp(); |
||
| 119 | $this->setToken(); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * set the timestamp. |
||
| 124 | */ |
||
| 125 | private function setTimestamp() |
||
| 126 | { |
||
| 127 | $this->timestamp = date('YmdHis'); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * set the token. |
||
| 132 | */ |
||
| 133 | private function setToken() |
||
| 134 | { |
||
| 135 | $this->token = md5($this->config['password']); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * invoke if SMS being sent is of type subscribable. |
||
| 140 | * |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | public function subscribable() |
||
| 144 | { |
||
| 145 | $this->message_type = MessageType::SUBSCIBABLE; |
||
| 146 | |||
| 147 | return $this; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * invoke if SMS being sent is of type on demand. |
||
| 152 | * |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | public function onDemand() |
||
| 156 | { |
||
| 157 | $this->message_type = MessageType::ON_DEMAND; |
||
| 158 | |||
| 159 | return $this; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * invoke if SMS being sent is of type bulk SMS. This will be the common one. |
||
| 164 | * |
||
| 165 | * @return $this |
||
| 166 | */ |
||
| 167 | public function bulk() |
||
| 168 | { |
||
| 169 | $this->message_type = MessageType::BULK; |
||
| 170 | |||
| 171 | return $this; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * invoke if SMS is being sent to a single recipient. |
||
| 176 | * |
||
| 177 | * @return $this |
||
| 178 | */ |
||
| 179 | public function toOne() |
||
| 180 | { |
||
| 181 | $this->batch_type = BatchType::NOT_BATCH; |
||
| 182 | |||
| 183 | return $this; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * invoke if SMS is being sent to a different recipients, or if each recipient receives custom sms. |
||
| 188 | * |
||
| 189 | * @return $this |
||
| 190 | */ |
||
| 191 | public function toMany() |
||
| 192 | { |
||
| 193 | $this->batch_type = BatchType::BATCH; |
||
| 194 | |||
| 195 | return $this; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param $recipients |
||
| 200 | * @param $message |
||
| 201 | * |
||
| 202 | * @throws BongaTechException |
||
| 203 | * |
||
| 204 | * @return mixed |
||
| 205 | */ |
||
| 206 | public function send($recipients, $message) |
||
| 207 | { |
||
| 208 | $this->recipients = $recipients; |
||
| 209 | $this->message = $message; |
||
| 210 | $this->endpoint = self::BASE_URL.self::SMS_ENDPOINT; |
||
| 211 | |||
| 212 | if ($this->batch_type === BatchType::NOT_BATCH) { |
||
| 213 | View Code Duplication | if (is_array($this->message) && array_depth($this->message) == 2 && count($this->message) == 1) { |
|
| 214 | if (is_array($this->recipients) && array_depth($this->recipients) == 2 && count($this->recipients) == 1) { |
||
| 215 | $response = $this->sendForNonBatch($this->buildSendObject($this->recipients, $this->message)); |
||
| 216 | } else { |
||
| 217 | throw new BongaTechException('The recipient MUST be an array of depth 2 and count should not be more than 1'); |
||
| 218 | } |
||
| 219 | } else { |
||
| 220 | throw new BongaTechException('Message should be provided as an array whose depth is 2 and count should equal 1'); |
||
| 221 | } |
||
| 222 | } elseif ($this->batch_type === BatchType::BATCH) { |
||
| 223 | View Code Duplication | if (is_array($this->message) && array_depth($this->message) == 2 && count($this->message) > 0) { |
|
| 224 | if (is_array($this->recipients) && array_depth($this->recipients) == 2 && count($this->recipients) > 1) { |
||
| 225 | $response = $this->sendForBatch($this->buildSendObject($this->recipients, $this->message)); |
||
| 226 | } else { |
||
| 227 | throw new BongaTechException('The recipients MUST be an array of depth 2 and count should be more than 1'); |
||
| 228 | } |
||
| 229 | } else { |
||
| 230 | throw new BongaTechException('Message should be provided as an array whose depth should be 2'); |
||
| 231 | } |
||
| 232 | } else { |
||
| 233 | throw new BongaTechException('Message Batch Type has not been set.'); |
||
| 234 | } |
||
| 235 | |||
| 236 | return $response; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * build the send object. |
||
| 241 | * |
||
| 242 | * @param recipients |
||
| 243 | * @param $messages |
||
| 244 | * |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | private function buildSendObject($recipients, $messages) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * send a message to a single recipient. |
||
| 282 | * |
||
| 283 | * @param $body |
||
| 284 | * |
||
| 285 | * @return Response |
||
| 286 | */ |
||
| 287 | private function sendForNonBatch($body) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * send batch. 1) same message to many recipients 2) different messages to many recipients. |
||
| 297 | * |
||
| 298 | * @param $body |
||
| 299 | * |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | private function sendForBatch($body) |
||
| 317 | |||
| 318 | public static function getBalance() |
||
| 329 | } |
||
| 330 |