SmsOnline::tryConnectToProvider()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace HoomanMirghasemi\Sms\Drivers;
4
5
use Exception;
6
use HoomanMirghasemi\Sms\Abstracts\Driver;
7
use Illuminate\Support\Facades\Log;
8
use SoapClient;
9
10
class SmsOnline extends Driver
11
{
12
    private SoapClient $soapClient;
13
14
    public function __construct(protected array $settings)
15
    {
16
        $this->from = data_get($this->settings, 'from');
17
        $this->tryConnectToProvider();
18
    }
19
20
    /**
21
     * Send sms method for OnlineSms.
22
     *
23
     * This method send sms and save log to db.
24
     *
25
     * @return bool
26
     */
27
    public function send(): bool
28
    {
29
        if (!$this->serviceActive) {
30
            parent::failedConnectToProvider();
31
32
            return false;
33
        }
34
        $response = $this->soapClient->SendSms([
35
            'username' => data_get($this->settings, 'username'),
36
            'password' => data_get($this->settings, 'password'), // Credientials
37
            'from'     => data_get($this->settings, 'from'),
38
            'to'       => [str_replace('+98', '', $this->recipient)],
39
            'text'     => $this->getMessage(),
40
            'isflash'  => false,
41
            'udh'      => '',
42
            'recId'    => [0],
43
            'status'   => [0],
44
        ]);
45
        if ($response->SendSmsResult != 1) {
46
            $this->webserviceResponse = 'An error occured';
47
            $this->webserviceResponse .= 'Error Code : '.$this->getErrors()[$response->SendSmsResult];
48
            $this->success = false;
49
        } else {
50
            $this->webserviceResponse = 'Message has been successfully sent ; MessageId : '.$response->recId->long;
51
            $this->success = true;
52
        }
53
54
        return parent::send();
55
    }
56
57
    /**
58
     * Return the remaining balance of smsonline.
59
     *
60
     * @return string
61
     */
62
    public function getBalance(): string
63
    {
64
        if (!$this->serviceActive) {
65
            return 'ماژول پیامک آنلاین اس ام اس برنامه غیر فعال می باشد.';
66
        }
67
68
        try {
69
            $getCreditResult = $this->soapClient->GetCredit([
70
                'username' => data_get($this->settings, 'username'),
71
                'password' => data_get($this->settings, 'password'),
72
            ]);
73
74
            return (int) ceil($getCreditResult->GetCreditResult);
75
        } catch (Exception $e) {
76
            return 'message:'.$e->getMessage().' code: '.$e->getCode();
77
        }
78
    }
79
80
    /**
81
     * Return error messages for SmsMagfa.
82
     *
83
     * @return array
84
     */
85
    private function getErrors(): array
86
    {
87
        $errors = [];
88
        $errors[0] = 'invalid username or password';
89
        $errors[1] = 'there is no error, sms successfully sent';
90
        $errors[2] = 'not enough credit';
91
        $errors[3] = 'limit in daily send';
92
        $errors[4] = 'limit in volume send';
93
        $errors[5] = 'sender number is not correct or invalid';
94
        $errors[6] = 'there is no correct number for send sms';
95
        $errors[7] = 'sms content is empty';
96
        $errors[8] = 'sender user or creator of sender is inactive';
97
        $errors[9] = 'numbers is to much';
98
        $errors[100] = 'you are not valid to send';
99
100
        return $errors;
101
    }
102
103
    /**
104
     * Make SoapClient object and connect to magfa wsdl webservices.
105
     *
106
     * @return void
107
     */
108
    private function tryConnectToProvider(): void
109
    {
110
        try {
111
            $this->soapClient = new SoapClient(data_get($this->settings, 'wsdl_url'));
112
        } catch (\SoapFault $soapFault) {
113
            Log::error('onlinesms sms code: '.$soapFault->getCode().' message: '.$soapFault->getMessage());
114
            $this->serviceActive = false;
115
        }
116
    }
117
}
118