BetaSms::send()   B
last analyzed

Complexity

Conditions 6
Paths 20

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9.2805

Importance

Changes 0
Metric Value
cc 6
nc 20
nop 1
dl 0
loc 32
ccs 11
cts 20
cp 0.55
crap 9.2805
rs 8.7857
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Djunehor
5
 * Date: 1/22/2019
6
 * Time: 9:36 AM.
7
 */
8
9
namespace Djunehor\Sms\Concrete;
10
11
use GuzzleHttp\Exception\ClientException;
12
use GuzzleHttp\Psr7\Request;
13
14
class BetaSms extends Sms
15
{
16
    private $baseUrl = 'http://login.betasms.com.ng/';
17
18
    /**
19
     * Class Constructor.
20
     * @param null $message
21
     */
22 7 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...
23
    {
24 7
        $this->username = config('laravel-sms.beta_sms.username');
25 7
        $this->password = config('laravel-sms.beta_sms.password');
26
27 7
        if ($message) {
28
            $this->text($message);
29
        }
30
31 7
        $this->client = $this->getInstance();
32 7
        $this->request = new Request('POST', $this->baseUrl.'api/');
33 7
    }
34
35
    /**
36
     * @param null $text
37
     * @return bool
38
     */
39 1
    public function send($text = null): bool
40
    {
41 1
        if ($text) {
42
            $this->setText($text);
43
        }
44
        try {
45 1
            $response = $this->client->send($this->request, [
46
                'form_params' => [
47 1
                    'mobiles' => implode(',', $this->recipients),
48 1
                    'sender' => $this->sender ?? config('laravel-sms.sender'),
49 1
                    'username' => $this->username,
50 1
                    'password' => $this->password,
51 1
                    'message' => $this->text,
52
                ],
53
            ]);
54
55 1
            $response = json_decode($response->getBody()->getContents(), true);
56 1
            $this->response = array_key_exists('status', $response) ? $response['status'] : $response['error'];
57
58 1
            return $this->response == 'OK' ? true : false;
59
        } catch (ClientException $e) {
60
            logger()->error('HTTP Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
61
            $this->httpError = $e;
62
63
            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