Passed
Branch master (b20890)
by Ridvan Lakas ng Bayan
02:07
created

Nexmo::send()   B

Complexity

Conditions 3
Paths 16

Size

Total Lines 33
Code Lines 23

Duplication

Lines 5
Ratio 15.15 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 5
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
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
     */
33
    public function send($phoneNumber, $message)
34
    {
35
        try {
36
            $conf = Config::load(__DIR__ . '/../config/providers.json')[$this->className];
37
38
            $query = array(
39
                'api_key' => $conf['api_key'],
40
                'api_secret' => $conf['api_secret'],
41
                'from' => $conf['from'],
42
                'to' => $phoneNumber,
43
                'text' => $message,
44
            );
45
46
            $query = http_build_query($query);
47
            $ch = curl_init();
48
            curl_setopt($ch, CURLOPT_URL, $conf['url']);
49
            curl_setopt($ch, CURLOPT_POST, count($query));
50
            curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
51
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
52
            $result = curl_exec($ch);
53
            curl_close($ch);
54
55
            $result = json_decode($result);
56
57 View Code Duplication
            if (intval($result->messages[0]->status) === 0) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
58
                $this->response(200, $result, null, $this->className);
59
            } else {
60
                $this->response(500, $result, null, $this->className);
61
            }
62
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class ridvanbaluyos\sms\providers\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
63
64
        }
65
    }
66
}
67