Passed
Push — master ( fa1980...7d6da9 )
by Zacchaeus
01:11 queued 10s
created

BetaSms::send()   B

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\Client;
12
use GuzzleHttp\Exception\ClientException;
13
use Illuminate\Support\Facades\Log;
14
15
class BetaSms extends Sms
16
{
17
    private $baseUrl = 'http://login.betasms.com.ng/';
18
19
    /**
20
     * Class Constructor.
21
     * @param null $message
22
     */
23 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...
24
    {
25 7
        $this->username = config('laravel-sms.beta_sms.username');
26 7
        $this->password = config('laravel-sms.beta_sms.password');
27
28 7
        if ($message) {
29
            $this->text($message);
30
        }
31
32 7
        $this->client = new Client([
33 7
            'base_uri' => $this->baseUrl,
34
            'headers' => [
35
                'header' => 'Content-type: application/x-www-form-urlencoded',
36
                'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36 OPR/47.0.2631.39',
37
            ],
38
        ]);
39 7
    }
40
41
    /**
42
     * @param null $text
43
     * @return bool
44
     */
45 1
    public function send($text = null): bool
46
    {
47 1
        if ($text) {
48
            $this->setText($text);
49
        }
50
        try {
51 1
            $response = $this->client->post('api/', [
52
                'query' => [
53 1
                    'mobiles' => implode(',', $this->recipients),
54 1
                    'sender' => $this->sender ?? config('laravel-sms.sender'),
55 1
                    'username' => $this->username,
56 1
                    'password' => $this->password,
57 1
                    'message' => $this->text,
58
                ],
59
            ]);
60
61 1
            $response = json_decode($response->getBody()->getContents(), true);
62 1
            $this->response = array_key_exists('status', $response) ? $response['status'] : $response['error'];
63
64 1
            return $this->response == 'OK' ? true : false;
65
        } catch (ClientException $e) {
66
            Log::info('HTTP Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
67
            $this->httpError = $e;
68
69
            return false;
70
        } catch (\Exception $e) {
71
            Log::info('SMS Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
72
            $this->httpError = $e;
73
74
            return false;
75
        }
76
    }
77
}
78