| Total Complexity | 40 |
| Total Lines | 368 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Citcall often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Citcall, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Citcall |
||
| 27 | { |
||
| 28 | use ConfigCollectorTrait; |
||
| 29 | use ErrorCollectorTrait; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Citcall::$response |
||
| 33 | * |
||
| 34 | * Citcall original response. |
||
| 35 | * |
||
| 36 | * @access protected |
||
| 37 | * @type mixed |
||
| 38 | */ |
||
| 39 | protected $response; |
||
| 40 | |||
| 41 | // ------------------------------------------------------------------------ |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Citcall::__construct |
||
| 45 | * |
||
| 46 | * @param array $config |
||
| 47 | * |
||
| 48 | * @access public |
||
| 49 | */ |
||
| 50 | public function __construct(array $config = []) |
||
| 51 | { |
||
| 52 | $defaultConfig = [ |
||
| 53 | 'apiUrl' => 'https://gateway.citcall.com/', |
||
| 54 | 'version' => 'v3', |
||
| 55 | 'appName' => null, |
||
| 56 | 'userId' => null, |
||
| 57 | 'senderId' => null, |
||
| 58 | 'apiKey' => null, |
||
| 59 | 'retry' => 0, |
||
| 60 | ]; |
||
| 61 | |||
| 62 | $this->setConfig(array_merge($defaultConfig, $config)); |
||
| 63 | } |
||
| 64 | // ------------------------------------------------------------------------ |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Citcall::setApiUrl |
||
| 68 | * |
||
| 69 | * Set Citcall API Url. |
||
| 70 | * |
||
| 71 | * @param string $serverIp Citcall API Url. |
||
| 72 | * |
||
| 73 | * @access public |
||
| 74 | * @return static |
||
| 75 | */ |
||
| 76 | public function setApiUrl($apiUrl) |
||
| 81 | } |
||
| 82 | // ------------------------------------------------------------------------ |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Citcall::setAppName |
||
| 86 | * |
||
| 87 | * Set Citcall App Name. |
||
| 88 | * |
||
| 89 | * @param string $appName Application Name |
||
| 90 | * |
||
| 91 | * @access public |
||
| 92 | * @return static |
||
| 93 | */ |
||
| 94 | public function setAppName($appName) |
||
| 95 | { |
||
| 96 | $this->setConfig('appName', $appName); |
||
| 97 | |||
| 98 | return $this; |
||
| 99 | } |
||
| 100 | // ------------------------------------------------------------------------ |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Citcall::setUserId |
||
| 104 | * |
||
| 105 | * Set Citcall User Id. |
||
| 106 | * |
||
| 107 | * @param string $userId Citcall User id |
||
| 108 | * |
||
| 109 | * @access public |
||
| 110 | * @return static |
||
| 111 | */ |
||
| 112 | public function setUserId($userId) |
||
| 113 | { |
||
| 114 | $this->setConfig('userId', $userId); |
||
| 115 | |||
| 116 | return $this; |
||
| 117 | } |
||
| 118 | // ------------------------------------------------------------------------ |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Citcall::setUserId |
||
| 122 | * |
||
| 123 | * Set Citcall User Id. |
||
| 124 | * |
||
| 125 | * @param string $userId Citcall Sender ID |
||
| 126 | * |
||
| 127 | * @access public |
||
| 128 | * @return static |
||
| 129 | */ |
||
| 130 | public function setSenderId($senderId) |
||
| 131 | { |
||
| 132 | $this->setConfig('senderId', $senderId); |
||
| 133 | |||
| 134 | return $this; |
||
| 135 | } |
||
| 136 | // ------------------------------------------------------------------------ |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Citcall::setApiKey |
||
| 140 | * |
||
| 141 | * Set Citcall API Key. |
||
| 142 | * |
||
| 143 | * @param string $apiKey Citcall API Key |
||
| 144 | * |
||
| 145 | * @access public |
||
| 146 | * @return static |
||
| 147 | */ |
||
| 148 | public function setApiKey($apiKey) |
||
| 149 | { |
||
| 150 | $this->setConfig('apiKey', $apiKey); |
||
| 151 | |||
| 152 | return $this; |
||
| 153 | } |
||
| 154 | // ------------------------------------------------------------------------ |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Citcall::request |
||
| 158 | * |
||
| 159 | * Call API request. |
||
| 160 | * |
||
| 161 | * @param string $path |
||
| 162 | * @param array $params |
||
| 163 | * @param string $type |
||
| 164 | * |
||
| 165 | * @access protected |
||
| 166 | * @return mixed |
||
| 167 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException |
||
| 168 | */ |
||
| 169 | protected function request($path, $params = [], $type = 'GET') |
||
| 207 | } |
||
| 208 | // ------------------------------------------------------------------------ |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Citcall::buildSendPackageData |
||
| 212 | * |
||
| 213 | * @param array $data |
||
| 214 | * |
||
| 215 | * @return array|bool |
||
| 216 | */ |
||
| 217 | protected function validateMsisdn($msisdn) |
||
| 218 | { |
||
| 219 | if (preg_match('/^(62[1-9]{1}[0-9]{1,2})[0-9]{6,9}$/', $msisdn) == 1) { |
||
| 220 | $msisdn = '0' . substr($msisdn, 2); |
||
| 221 | } elseif (preg_match('/^(\+62[1-9]{1}[0-9]{1,2})[0-9]{6,9}$/', $msisdn) == 1) { |
||
| 222 | $msisdn = '0' . substr($msisdn, 3); |
||
| 223 | } |
||
| 224 | |||
| 225 | if (preg_match('/^(0[1-9]{1}[0-9]{1,2})[0-9]{6,9}$/', $msisdn) == 1) { |
||
| 226 | return trim($msisdn); |
||
| 227 | } |
||
| 228 | |||
| 229 | return false; |
||
| 230 | } |
||
| 231 | // ------------------------------------------------------------------------ |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Citcall::send |
||
| 235 | * |
||
| 236 | * Send SMS |
||
| 237 | * |
||
| 238 | * @param string $msisdn MSISDN Number |
||
| 239 | * @param string $message Message |
||
| 240 | * |
||
| 241 | * @access public |
||
| 242 | * @return mixed |
||
| 243 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException |
||
| 244 | */ |
||
| 245 | public function send($msisdn, $message) |
||
| 258 | } |
||
| 259 | // ------------------------------------------------------------------------ |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Citcall::missedCallOtp |
||
| 263 | * |
||
| 264 | * Async Missed Call |
||
| 265 | * |
||
| 266 | * @param string $msisdn MSISDN Number |
||
| 267 | * @param string $gateway Gateway Number |
||
| 268 | * @param bool $async Asyncronous call |
||
| 269 | * |
||
| 270 | * @access public |
||
| 271 | * @return mixed |
||
| 272 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException |
||
| 273 | */ |
||
| 274 | public function missedCallOtp($msisdn, $gateway = 1, $async = false) |
||
| 275 | { |
||
| 276 | if (false === ($msisdn = $this->validateMsisdn($msisdn))) { |
||
| 277 | throw new \InvalidArgumentException('Citcall: Invalid MSISDN Number'); |
||
| 278 | } |
||
| 279 | |||
| 280 | if ( ! is_int($gateway) or $gateway > 5 or $gateway < 0) { |
||
| 281 | throw new \InvalidArgumentException('Citcall: Invalid Gateway Number'); |
||
| 282 | } |
||
| 283 | |||
| 284 | $path = $async === true ? 'asynccall' : 'call'; |
||
| 285 | |||
| 286 | return $this->request($path, [ |
||
| 287 | 'msisdn' => $msisdn, |
||
| 288 | 'gateway' => (int)$gateway, |
||
| 289 | ], 'POST'); |
||
| 290 | } |
||
| 291 | // ------------------------------------------------------------------------ |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Citcall::sendOtp |
||
| 295 | * |
||
| 296 | * Send SMS |
||
| 297 | * |
||
| 298 | * @param string $msisdn MSISDN Number |
||
| 299 | * @param string $token OTP Token Code |
||
| 300 | * @param int $expires Expires time in seconds |
||
| 301 | * |
||
| 302 | * @access public |
||
| 303 | * @return mixed |
||
| 304 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException |
||
| 305 | */ |
||
| 306 | public function sendOtp($msisdn, $token, $expires = 0) |
||
| 330 | } |
||
| 331 | // ------------------------------------------------------------------------ |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Citcall::verifyOtp |
||
| 335 | * |
||
| 336 | * @param string $trxId |
||
| 337 | * @param string $msisdn |
||
| 338 | * @param string $token |
||
| 339 | */ |
||
| 340 | public function verifyOtp($trxId, $msisdn, $token) |
||
| 354 | } |
||
| 355 | // ------------------------------------------------------------------------ |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Citcall::getResponse |
||
| 359 | * |
||
| 360 | * Get original response object. |
||
| 361 | * |
||
| 362 | * @access public |
||
| 363 | * @return \O2System\Curl\Response|bool Returns FALSE if failed. |
||
| 364 | */ |
||
| 365 | public function getResponse() |
||
| 366 | { |
||
| 367 | return $this->response; |
||
| 368 | } |
||
| 369 | // ------------------------------------------------------------------------ |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Citcall::getCallback |
||
| 373 | * |
||
| 374 | * Get callback json response from Citcall |
||
| 375 | * |
||
| 376 | * @return \O2System\Curl\Response\SimpleJSONElement |
||
| 377 | */ |
||
| 378 | public function getCallback() |
||
| 394 | } |
||
| 395 | } |
||
| 396 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.