Semaphore::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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 Noodlehaus\Config as Config;
7
use Exception as Exception;
8
9
/**
10
 * Class Semaphore
11
 * @package ridvanbaluyos\sms\providers
12
 */
13
class Semaphore extends Sms implements SmsProviderServicesInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $className;
19
20
    /**
21
     * Semaphore 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
            $query = [
40
                'from' => $conf['from'],
41
                'api' => $conf['api'],
42
                'number' => $phoneNumber,
43
                'message' => $message,
44
            ];
45
46
            $url = $conf['url'] . '?' . http_build_query($query);
47
48
            $ch = curl_init();
49
            curl_setopt($ch,CURLOPT_URL,$url);
50
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
51
            curl_setopt($ch, CURLOPT_TIMEOUT, 240);
52
            $result = curl_exec($ch);
53
            curl_close($ch);
54
55
            $result = json_decode($result);
56
57
            if ($result->status === 'success') {
58
                return $this->response(202, $result, null, $this->className);
59
            } else {
60
                return $this->response(500, $result->message, 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...
63
64
        }
65
    }
66
67
    /**
68
     * This function gets the account balance.
69
     *
70
     */
71 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...
72
    {
73
        try {
74
            $conf = Config::load(__DIR__ . '/../config/providers.json')[$this->className];
75
            $query = [
76
                'api' => $conf['api'],
77
            ];
78
79
            $url = $conf['url'] . '/account?' . http_build_query($query);
80
81
            $ch = curl_init();
82
            curl_setopt($ch,CURLOPT_URL,$url);
83
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
84
            curl_setopt($ch, CURLOPT_TIMEOUT, 240);
85
            $result = curl_exec($ch);
86
            curl_close($ch);
87
88
            $result = json_decode($result);
89
90
            if ($result->status === 'success') {
91
                return $this->response(200, $result, null, $this->className);
92
            } else {
93
                return $this->response(500, $result->message, null, $this->className);
94
            }
95
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
96
97
        }
98
    }
99
}