Passed
Push — master ( 7d6da9...87fa1f )
by Zacchaeus
01:25 queued 10s
created

AfricasTalking   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 18.75 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
dl 12
loc 64
ccs 18
cts 30
cp 0.6
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 2
A send() 0 38 5

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Djunehor\Sms\Concrete;
4
5
use GuzzleHttp\Exception\ClientException;
6
use GuzzleHttp\Psr7\Request;
7
8
class AfricasTalking extends Sms
9
{
10
    private $baseUrl = 'https://api.africastalking.com/';
11
12
    /**
13
     * Class Constructor.
14
     * @param null $message
15
     */
16 1 View Code Duplication
    public function __construct($message = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
    {
18 1
        if ($message) {
19
            $this->text($message);
20
        }
21 1
        $this->client = $this->getInstance();
22
        $headers = [
23 1
          'apiKey' => config('laravel-sms.africas_talking.api_key'),
24 1
            'Content-Type' => 'application/x-www-form-urlencoded',
25
        ];
26 1
        $this->request = new Request('POST', $this->baseUrl.'version1/messaging', $headers);
27 1
    }
28
29
    /**
30
     * @param null $text
31
     * @return bool
32
     */
33 1
    public function send($text = null): bool
34
    {
35 1
        if ($text) {
36
            $this->setText($text);
37
        }
38
        try {
39 1
            $request = $this->client->send($this->request, [
40
                'form_params' => [
41 1
                    'username' => config('laravel-sms.africas_talking.username', 'djunehor'),
42 1
                   'from' => $this->sender ?? config('laravel-sms.sender'),
43 1
                    'to' => implode(',', $this->recipients),
44 1
                    'message' => $this->text,
45
                ],
46
            ]);
47
48
            $response = json_decode($request->getBody()->getContents(), true);
49
50
            if (! empty($response['SMSMessageData']['Recipients'])) {
51
                $this->response = $response['SMSMessageData']['Message'];
52
53
                return true;
54
            }
55
56
            $this->response = $response['SMSMessageData']['Message'];
57
58
            return false;
59 1
        } catch (ClientException $e) {
60 1
            logger()->error('HTTP Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
61 1
            $this->httpError = $e;
62
63 1
            return false;
64
        } catch (\Exception $e) {
65
            logger()->error('SMS Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
66
            $this->httpError = $e;
67
68
            return false;
69
        }
70
    }
71
}
72