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

RingCaptcha::send()   A

Complexity

Conditions 5
Paths 20

Size

Total Lines 38

Duplication

Lines 38
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 8.125

Importance

Changes 0
Metric Value
cc 5
nc 20
nop 1
dl 38
loc 38
ccs 11
cts 22
cp 0.5
crap 8.125
rs 9.0008
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 RingCaptcha extends Sms
9
{
10
    private $baseUrl = 'https://api.ringcaptcha.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
        $this->username = config('laravel-sms.ring_captcha.app_key');
19 1
        $this->password = config('laravel-sms.ring_captcha.api_key');
20 1
        if ($message) {
21
            $this->text($message);
22
        }
23 1
        $this->client = self::getInstance();
24 1
        $this->request = new Request('POST', $this->baseUrl."$this->username/sms");
25 1
    }
26
27
    /**
28
     * @param null $text
29
     * @return bool
30
     */
31 1 View Code Duplication
    public function send($text = null): bool
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...
32
    {
33 1
        if ($text) {
34
            $this->setText($text);
35
        }
36
        try {
37 1
            $request = $this->client->send($this->request, [
38
                'form_params' => [
39 1
                    'phone' => implode(',', $this->recipients),
40 1
                    'app_key' => $this->username,
41 1
                    'api_key' => $this->password,
42 1
                    'message' => $this->text,
43
                ],
44
            ]);
45
46
            $response = json_decode($request->getBody()->getContents(), true);
47
48
            if ($response['status'] == 'SUCCESS') {
49
                $this->response = 'The message was sent successfully';
50
51
                return true;
52
            }
53
54
            $this->response = $response['message'];
55
56
            return false;
57 1
        } catch (ClientException $e) {
58 1
            logger()->error('HTTP Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
59 1
            $this->httpError = $e;
60
61 1
            return false;
62
        } catch (\Exception $e) {
63
            logger()->error('SMS Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
64
            $this->httpError = $e;
65
66
            return false;
67
        }
68
    }
69
}
70