Avanak::getBalance()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 12
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
use SoapFault;
10
11
class Avanak extends Driver
12
{
13
    private SoapClient $client;
14
15
    private array $params;
16
17
    public function __construct(protected array $settings)
18
    {
19
        $this->from = data_get($this->settings, 'from');
20
21
        $this->params['userName'] = data_get($this->settings, 'username');
22
        $this->params['password'] = data_get($this->settings, 'password');
23
        $this->params['vote'] = false;
24
        $this->params['serverid'] = $this->from;
25
26
        $this->tryConnectToProvider();
27
    }
28
29
    /**
30
     * Send voice call method for Avanak.
31
     *
32
     * This method send sms and save log to db.
33
     *
34
     * @return bool
35
     */
36
    public function send(): bool
37
    {
38
        if (!$this->serviceActive) {
39
            parent::failedConnectToProvider();
40
41
            return false;
42
        }
43
        $this->params['text'] = $this->getMessage();
44
        $this->params['number'] = $this->recipient;
45
        if (!str_starts_with($this->params['number'], '+98')) {
46
            return false;
47
        }
48
        // for sending to avanak change number format
49
        $this->params['number'] = str_replace('+98', '0', $this->params['number']);
50
51
        try {
52
            $response = $this->client->QuickSendWithTTS($this->params);
53
            if ($response->QuickSendWithTTSResult > 0) {
54
                $this->webserviceResponse = 'Send message id :'.$response->QuickSendWithTTSResult;
55
                $this->success = true;
56
            } else {
57
                $this->webserviceResponse = 'Error Code : '.$response->QuickSendWithTTSResult;
58
                $this->success = false;
59
            }
60
        } catch (Exception $e) {
61
            $this->webserviceResponse = 'code:'.$e->getCode().' message: '.$e->getMessage();
62
            $this->success = false;
63
        }
64
65
        return parent::send();
66
    }
67
68
    /**
69
     * Return the remaining balance of avanak.
70
     *
71
     * @return string
72
     */
73
    public function getBalance(): string
74
    {
75
        if (!$this->serviceActive) {
76
            return 'وب سرویس آوانک با مشکل مواجه شده.';
77
        }
78
79
        try {
80
            $response = $this->client->GetCredit($this->params);
81
82
            return $response->GetCreditResult;
83
        } catch (Exception $e) {
84
            return 'message:'.$e->getMessage().' code: '.$e->getCode();
85
        }
86
    }
87
88
    /**
89
     * Make SoapClient object and connect to avanak wsdl webservices.
90
     *
91
     * @return void
92
     */
93
    private function tryConnectToProvider(): void
94
    {
95
        try {
96
            $this->client = new SoapClient(data_get($this->settings, 'wsdl_url'), ['trace' => 1, 'encoding' => 'UTF-8']);
97
        } catch (SoapFault $soapFault) {
98
            Log::error('avanak voice call code: '.$soapFault->getCode().' message: '.$soapFault->getMessage());
99
            $this->serviceActive = false;
100
        }
101
    }
102
}
103