Nexmo::send()   B
last analyzed

Complexity

Conditions 3
Paths 16

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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