| Total Complexity | 43 |
| Total Lines | 267 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Client |
||
| 15 | { |
||
| 16 | use WritesHttpLogs; |
||
| 17 | |||
| 18 | const PROVIDER_BANGLALINK = Providers\Banglalink::class; |
||
| 19 | const PROVIDER_BD_WEB_HOST_24 = Providers\BdWebHost24::class; |
||
| 20 | const PROVIDER_BOOM_CAST = Providers\BoomCast::class; |
||
| 21 | const PROVIDER_ELITBUZZ = Providers\Elitbuzz::class; |
||
| 22 | const PROVIDER_GRAMEENPHONE = Providers\Grameenphone::class; |
||
| 23 | const PROVIDER_NOVOCOM = Providers\Novocom::class; |
||
| 24 | const PROVIDER_PAYSTATION = Providers\Paystation::class; |
||
| 25 | const PROVIDER_ROBI = Providers\Robi::class; |
||
| 26 | const PROVIDER_SSL = Providers\Ssl::class; |
||
| 27 | const PROVIDER_VALUE_FIRST = Providers\ValueFirst::class; |
||
| 28 | |||
| 29 | private $provider; |
||
| 30 | |||
| 31 | public function __construct(ProviderInterface $provider) |
||
| 32 | { |
||
| 33 | $this->provider = $provider; |
||
| 34 | $this->enableLogging = Config::get('sms-service-with-bd-providers::config.enable_api_call_logging', false); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Return a SMS provider according to the given provider name. |
||
| 39 | * |
||
| 40 | * @param string $providerName |
||
| 41 | * @param array $config |
||
| 42 | * @param string $url |
||
| 43 | * |
||
| 44 | * @return ProviderInterface |
||
| 45 | */ |
||
| 46 | public static function getProvider($providerName = self::PROVIDER_SSL, array $config = [], $url = null) |
||
| 47 | { |
||
| 48 | switch ($providerName) { |
||
| 49 | case self::PROVIDER_BANGLALINK: |
||
| 50 | case self::PROVIDER_BD_WEB_HOST_24: |
||
| 51 | case self::PROVIDER_BOOM_CAST: |
||
| 52 | case self::PROVIDER_ELITBUZZ: |
||
| 53 | case self::PROVIDER_GRAMEENPHONE: |
||
| 54 | case self::PROVIDER_NOVOCOM: |
||
| 55 | case self::PROVIDER_PAYSTATION: |
||
| 56 | case self::PROVIDER_ROBI: |
||
| 57 | case self::PROVIDER_SSL: |
||
| 58 | case self::PROVIDER_VALUE_FIRST: |
||
| 59 | return new $providerName($config, $url); |
||
| 60 | |||
| 61 | default: |
||
| 62 | throw new Exception('Invalid SMS provider name is given.'); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | public function send($recipients, $message, array $params = []) |
||
| 109 | } |
||
| 110 | |||
| 111 | public function sendWithFallback($recipients, $message, array $params = []) |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Prepare the curl options array according to the given data. |
||
| 185 | * |
||
| 186 | * @param array $data |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | private function prepareCurlOptions(array $data) |
||
| 191 | { |
||
| 192 | $options = [ |
||
| 193 | 'timeout' => 30, |
||
| 194 | ]; |
||
| 195 | |||
| 196 | switch(get_class($this->provider)) { |
||
| 197 | case self::PROVIDER_GRAMEENPHONE: |
||
| 198 | case self::PROVIDER_NOVOCOM: |
||
| 199 | $options += [ |
||
| 200 | 'httpheader' => ['Content-Type: application/json'], |
||
| 201 | 'post' => 1, |
||
| 202 | 'postfields' => json_encode($data), |
||
| 203 | ]; |
||
| 204 | break; |
||
| 205 | |||
| 206 | case self::PROVIDER_PAYSTATION: |
||
| 207 | $options += [ |
||
| 208 | 'httpheader' => [ |
||
| 209 | 'Content-Type: application/json', |
||
| 210 | 'Accept: application/json', |
||
| 211 | 'user_id:'.$data['user_id'], |
||
| 212 | 'password:'.$data['password'], |
||
| 213 | ], |
||
| 214 | 'post' => 1, |
||
| 215 | 'postfields' => json_encode($data), |
||
| 216 | ]; |
||
| 217 | break; |
||
| 218 | |||
| 219 | default: |
||
| 220 | $options += [ |
||
| 221 | 'post' => count($data), |
||
| 222 | 'postfields' => http_build_query($data), |
||
| 223 | ]; |
||
| 224 | } |
||
| 225 | |||
| 226 | if ($this->provider instanceof NeedsAuthenticationInterface) { |
||
| 227 | $options['httpheader'][] = 'Authorization: Bearer '.$this->provider->getAccessToken(); |
||
|
|
|||
| 228 | } |
||
| 229 | |||
| 230 | return $options; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Execute the Curl request according to the given curl options. |
||
| 235 | * |
||
| 236 | * @param array $options |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | private function executeWithCurl(array $options) |
||
| 267 | } |
||
| 268 | |||
| 269 | private function getSummaryWithLogs(array $log) |
||
| 284 |