Test Failed
Branch master (8efd2a)
by Ridvan Lakas ng Bayan
05:20 queued 02:59
created

Nexmo::balance()   B

Complexity

Conditions 4
Paths 14

Size

Total Lines 29
Code Lines 18

Duplication

Lines 29
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 29
loc 29
rs 8.5806
cc 4
eloc 18
nc 14
nop 0
1
<?php
2
namespace ridvanbaluyos\sms\providers;
3
4
use ridvanbaluyos\sms\SmsProviderServicesInterface as SmsProviderServicesInterface;
5
use ridvanbaluyos\sms\Sms as Sms;
6
use Exception as Exception;
7
8
/**
9
 * Class Nexmo
10
 * @package ridvanbaluyos\sms\providers
11
 */
12
class Nexmo extends Sms implements SmsProviderServicesInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $className;
18
19
    /**
20
     * Nexmo constructor.
21
     */
22
    public function __construct()
23
    {
24
        $this->className = substr(get_called_class(), strrpos(get_called_class(), '\\') + 1);
25
    }
26
27
    /**
28
     * This function sends the SMS.
29
     *
30
     * @param $phoneNumber
31
     * @param $message
32
     * @return string
33
     */
34
    public function send($phoneNumber, $message)
35
    {
36
        try {
37
            $conf = Config::load(__DIR__ . '/../config/providers.json')[$this->className];
38
39
            $query = array(
40
                'api_key' => $conf['api_key'],
41
                'api_secret' => $conf['api_secret'],
42
                'from' => $conf['from'],
43
                'to' => $phoneNumber,
44
                'text' => $message,
45
            );
46
47
            $query = http_build_query($query);
48
            $ch = curl_init();
49
            curl_setopt($ch, CURLOPT_URL, $conf['url'] . '/sms/json');
50
            curl_setopt($ch, CURLOPT_POST, count($query));
51
            curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
52
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
53
            $result = curl_exec($ch);
54
            curl_close($ch);
55
56
            $result = json_decode($result);
57
58
            if (intval($result->messages[0]->status) === 0) {
59
                return $this->response(200, $result, null, $this->className);
60
            } else {
61
                return $this->response(500, $result, null, $this->className);
62
            }
63
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
64
65
        }
66
    }
67
68
    /**
69
     * This function gets the account balance.
70
     *
71
     */
72 View Code Duplication
    public function balance()
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...
73
    {
74
        try {
75
            $conf = Config::load(__DIR__ . '/../config/providers.json')[$this->className];
76
77
            $query = array(
78
                'api_key' => $conf['api_key'],
79
                'api_secret' => $conf['api_secret']
80
            );
81
82
            $query = http_build_query($query);
83
84
            $ch = curl_init();
85
            curl_setopt($ch, CURLOPT_URL, $conf['url'] . '/account/get-balance?' . $query);
86
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
87
            $result = curl_exec($ch);
88
            curl_close($ch);
89
90
            $result = json_decode($result);
91
92
            if (is_float($result->value) || is_integer($result->value)) {
93
                return $this->response(200, $result, null, $this->className);
94
            } else {
95
                return $this->response(500, $result, null, $this->className);
96
            }
97
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
98
99
        }
100
    }
101
}
102