Twilio   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 64
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B send() 0 32 3
A balance() 0 4 1
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 Twilio
11
 * @package ridvanbaluyos\sms\providers
12
 */
13
class Twilio extends Sms implements SmsProviderServicesInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $className;
19
20
    /**
21
     * Twilio 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
        $phoneNumber = '+' . $phoneNumber;
38
        try {
39
            $conf = Config::load(__DIR__ . '/../config/providers.json')[$this->className];
40
41
            $query = array(
42
                'To' => $phoneNumber,
43
                'From' => $conf['from'],
44
                'Body' => $message,
45
            );
46
47
            $query = http_build_query($query);
48
            $ch = curl_init();
49
            curl_setopt($ch, CURLOPT_URL, $conf['url']);
50
            curl_setopt($ch, CURLOPT_POST, count($query));
51
            curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
52
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
53
            curl_setopt($ch, CURLOPT_USERPWD, "{$conf['account_sid']}:{$conf['auth_token']}");
54
            $result = curl_exec($ch);
55
            curl_close($ch);
56
            $result = json_decode($result);
57
58
            if (is_string($result->status)) {
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 checks the account balance.
70
     *
71
     */
72
    public function balance()
73
    {
74
        return $this->response(404, [], $this->className . ' currently does not support this feature.', $this->className);
75
    }
76
}
77