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

AfricasTalking::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 12
loc 12
ccs 7
cts 8
cp 0.875
crap 2.0078
rs 9.8666
c 0
b 0
f 0
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