Magfa::send()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 18
rs 9.9
1
<?php
2
3
namespace HoomanMirghasemi\Sms\Drivers;
4
5
use Exception;
6
use HoomanMirghasemi\Sms\Abstracts\Driver;
7
use Illuminate\Support\Facades\App;
8
use Illuminate\Support\Facades\Log;
9
use SoapClient;
10
use SoapFault;
11
12
class Magfa extends Driver
13
{
14
    private SoapClient $soapClient;
15
16
    public function __construct(protected array $settings)
17
    {
18
        $this->from = data_get($this->settings, 'from');
19
        $this->tryConnectToProvider();
20
    }
21
22
    /**
23
     * Send sms method for Magfa.
24
     *
25
     * This method send sms and save log to db.
26
     *
27
     * @return bool
28
     */
29
    public function send(): bool
30
    {
31
        if (!$this->serviceActive) {
32
            parent::failedConnectToProvider();
33
34
            return false;
35
        }
36
        $response = $this->soapClient->send([$this->getMessage()], [$this->from], [$this->recipient]);
37
        if ($response->status == 0) {
38
            $this->webserviceResponse = "Message has been successfully sent ; MessageId : {$response->messages->id}";
39
            $this->success = true;
40
        }
41
42
        $this->webserviceResponse = "An error occurred \n";
43
        $this->webserviceResponse .= "Error Code : $response->status ; Error Title : ".$this->getErrors()[$response->status]['title'].' {'.$this->getErrors()[$response->status]['desc'].'}';
44
        $this->success = false;
45
46
        return parent::send();
47
    }
48
49
    /**
50
     * Return the remaining balance of magfa.
51
     *
52
     * @return string
53
     */
54
    public function getBalance(): string
55
    {
56
        if (!$this->serviceActive) {
57
            return 'وب سرویس مگفا با مشکل مواجه شده.';
58
        }
59
60
        try {
61
            $response = $this->soapClient->balance();
62
63
            return $response->balance;
64
        } catch (Exception $e) {
65
            return 'message:'.$e->getMessage().' code: '.$e->getCode();
66
        }
67
    }
68
69
    /**
70
     * Make SoapClient object and connect to magfa wsdl webservices.
71
     *
72
     * @return void
73
     */
74
    private function tryConnectToProvider(): void
75
    {
76
        try {
77
            $this->soapClient = new SoapClient(data_get($this->settings, 'wsdl_url'), [
78
                'login'       => data_get($this->settings, 'username').'/'.data_get($this->settings, 'domain'),
79
                'password'    => data_get($this->settings, 'password'),
80
                'cache_wsdl'  => WSDL_CACHE_NONE, // -No WSDL Cache
81
                'compression' => (SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | 5), // -Compression *
82
                'trace'       => App::environment(['local', 'staging', 'testing']), // -Optional (debug)
83
            ]);
84
        } catch (SoapFault $soapFault) {
85
            Log::error('magfa sms code: '.$soapFault->getCode().' message: '.$soapFault->getMessage());
86
            $this->serviceActive = false;
87
        }
88
    }
89
90
    /**
91
     * Return error messages for SmsMagfa.
92
     *
93
     * @return array
94
     */
95
    private function getErrors(): array
96
    {
97
        $errors = [];
98
        $errors[1]['title'] = 'INVALID_RECIPIENT_NUMBER';
99
        $errors[1]['desc'] = 'the string you presented as recipient numbers are not valid phone numbers, please check them again';
100
101
        $errors[2]['title'] = 'INVALID_SENDER_NUMBER';
102
        $errors[2]['desc'] = 'the string you presented as sender numbers(3000-xxx) are not valid numbers, please check them again';
103
104
        $errors[3]['title'] = 'INVALID_ENCODING';
105
        $errors[3]['desc'] = 'are You sure You\'ve entered the right encoding for this message? You can try other encodings to bypass this error code';
106
107
        $errors[4]['title'] = 'INVALID_MESSAGE_CLASS';
108
        $errors[4]['desc'] = 'entered MessageClass is not valid. for a normal MClass, leave this entry empty';
109
110
        $errors[6]['title'] = 'INVALID_UDH';
111
        $errors[6]['desc'] = 'entered UDH is invalid. in order to send a simple message, leave this entry empty';
112
113
        $errors[12]['title'] = 'INVALID_ACCOUNT_ID';
114
        $errors[12]['desc'] = 'you\'re trying to use a service from another account??? check your UN/Password/NumberRange again';
115
116
        $errors[13]['title'] = 'NULL_MESSAGE';
117
        $errors[13]['desc'] = 'check the text of your message. it seems to be null';
118
119
        $errors[14]['title'] = 'CREDIT_NOT_ENOUGH';
120
        $errors[14]['desc'] = 'Your credit\'s not enough to send this message. you might want to buy some credit.call';
121
122
        $errors[15]['title'] = 'SERVER_ERROR';
123
        $errors[15]['desc'] = 'something bad happened on server side, you might want to call MAGFA Support about this:';
124
125
        $errors[16]['title'] = 'ACCOUNT_INACTIVE';
126
        $errors[16]['desc'] = 'Your account is not active right now, call -- to activate it';
127
128
        $errors[17]['title'] = 'ACCOUNT_EXPIRED';
129
        $errors[17]['desc'] = 'looks like Your account\'s reached its expiration time, call -- for more information';
130
131
        $errors[18]['title'] = 'INVALID_USERNAME_PASSWORD_DOMAIN'; // todo : note : one of them are empty
132
        $errors[18]['desc'] = 'the combination of entered Username/Password/Domain is not valid. check\'em again';
133
134
        $errors[19]['title'] = 'AUTHENTICATION_FAILED'; // todo : note : wrong arguments supplied ...
135
        $errors[19]['desc'] = 'You\'re not entering the correct combination of Username/Password';
136
137
        $errors[20]['title'] = 'SERVICE_TYPE_NOT_FOUND';
138
        $errors[20]['desc'] = 'check the service type you\'re requesting. we don\'t get what service you want to use. your sender number might be wrong, too.';
139
140
        $errors[22]['title'] = 'ACCOUNT_SERVICE_NOT_FOUND';
141
        $errors[22]['desc'] = 'your current number range does\'t have the permission to use Webservices';
142
143
        $errors[23]['title'] = 'SERVER_BUSY';
144
        $errors[23]['desc'] = 'Sorry, Server\'s under heavy traffic pressure, try testing another time please';
145
146
        $errors[24]['title'] = 'INVALID_MESSAGE_ID';
147
        $errors[24]['desc'] = 'entered message-id seems to be invalid, are you sure You entered the right thing?';
148
149
        $errors[25]['title'] = 'INVALID_METHOD_NAME';
150
        $errors[25]['desc'] = 'the method name call is invalid';
151
152
        $errors[27]['title'] = 'RECEIVER_NUMBER_IN_BLACK_LIST';
153
        $errors[27]['desc'] = 'the receiver number is in the black list';
154
155
        $errors[28]['title'] = 'PREFIX_RECEIVER_NUMBER_IS_BLOCKED';
156
        $errors[28]['desc'] = 'the receiver number is in the block list of magfa';
157
158
        $errors[29]['title'] = 'YOU_IP_ADDRESS_NOT_VALID';
159
        $errors[29]['desc'] = 'server ip address is not valid to use service';
160
161
        $errors[30]['title'] = 'LARGE_SMS_CONTENT';
162
        $errors[30]['desc'] = 'the sms content is more than 255 character';
163
164
        $errors[101]['title'] = 'WEB_RECIPIENT_NUMBER_ARRAY_SIZE_NOT_EQUAL_MESSAGE_CLASS_ARRAY_BODIES';
165
        $errors[101]['desc'] = 'this happens when you try to define MClasses for your messages. in this case you must define one recipient number for each MClass';
166
167
        $errors[102]['title'] = 'WEB_RECIPIENT_NUMBER_ARRAY_SIZE_NOT_EQUAL_MESSAGE_CLASS_ARRAY';
168
        $errors[102]['desc'] = 'this happens when you try to define MClasses for your messages. in this case you must define one recipient number for each MClass';
169
170
        $errors[103]['title'] = 'WEB_RECIPIENT_NUMBER_ARRAY_SIZE_NOT_EQUAL_SENDER_NUMBER_ARRAY';
171
        $errors[103]['desc'] = 'This error happens when you have more than one sender-number for message. when you have more than one sender number, for each sender-number you must define a recipient number...';
172
173
        $errors[104]['title'] = 'WEB_RECIPIENT_NUMBER_ARRAY_SIZE_NOT_EQUAL_MESSAGE_ARRAY';
174
        $errors[104]['desc'] = 'this happens when you try to define UDHs for your messages. in this case you must define one recipient number for each udh';
175
176
        $errors[105]['title'] = 'PRIORITIES_ARRAY_SIZE_NOT_EQUAL_RECEIVERS';
177
        $errors[105]['desc'] = 'priorities array size not equal receivers';
178
179
        $errors[106]['title'] = 'WEB_RECIPIENT_NUMBER_ARRAY_IS_NULL';
180
        $errors[106]['desc'] = 'array of recipient numbers must have at least one member';
181
182
        $errors[107]['title'] = 'WEB_RECIPIENT_NUMBER_ARRAY_TOO_LONG';
183
        $errors[107]['desc'] = 'the maximum number of recipients per message is 90';
184
185
        $errors[108]['title'] = 'WEB_SENDER_NUMBER_ARRAY_IS_NULL';
186
        $errors[108]['desc'] = 'array of sender numbers must have at least one member';
187
188
        $errors[109]['title'] = 'WEB_RECIPIENT_NUMBER_ARRAY_SIZE_NOT_EQUAL_ENCODING_ARRAY';
189
        $errors[109]['desc'] = 'this happens when you try to define encodings for your messages. in this case you must define one recipient number for each Encoding';
190
191
        $errors[110]['title'] = 'WEB_RECIPIENT_NUMBER_ARRAY_SIZE_NOT_EQUAL_CHECKING_MESSAGE_IDS__ARRAY';
192
        $errors[110]['desc'] = 'this happens when you try to define checking-message-ids for your messages. in this case you must define one recipient number for each checking-message-id';
193
194
        $errors[-1]['title'] = 'NOT_AVAILABLE';
195
        $errors[-1]['desc'] = 'The target of report is not available(e.g. no message is associated with entered IDs)';
196
197
        return $errors;
198
    }
199
}
200