AfricasTalking::getMessageID()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace CraftedSystems\LaravelSMS\Gateways;
4
5
use AfricasTalking\SDK\AfricasTalking as SMSGateway;
0 ignored issues
show
Bug introduced by
The type AfricasTalking\SDK\AfricasTalking was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use CraftedSystems\LaravelSMS\Contracts\SMSContract;
7
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class AfricasTalking implements SMSContract
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $settings;
15
16
    /**
17
     * @var bool
18
     */
19
    protected $is_success;
20
21
    /**
22
     * @var mixed
23
     */
24
    protected $message_id;
25
26
    /**
27
     * @var object
28
     */
29
    public $data;
30
31
    /**
32
     * @param $settings
33
     *
34
     * @throws \Exception
35
     */
36
    public function __construct($settings)
37
    {
38
        // initiate settings (username, api_key, etc)
39
40
        $this->settings = (object) $settings;
0 ignored issues
show
Documentation Bug introduced by
It seems like (object)$settings of type object is incompatible with the declared type array of property $settings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
    }
42
43
    /**
44
     * @param $recipient
45
     * @param $message
46
     * @param null $params
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $params is correct as it would always require null to be passed?
Loading history...
47
     *
48
     * @return object
49
     */
50
    public function send(string $recipient, string $message, $params = null)
51
    {
52
        $AT = new SMSGateway($this->settings->username, $this->settings->api_key);
53
        $sms = $AT->sms();
54
55
        $result = $sms->send([
56
            'to'      => $recipient,
57
            'message' => $message,
58
            'from'    => $this->settings->from,
59
        ]);
60
61
        // message sending successful
62
        if ($result['status'] == 'success') {
63
            $data = $result['data']->SMSMessageData->Recipients[0];
64
65
            $this->is_success = $data->status == 'Success'; // define what determines success from the response
66
            $this->message_id = $data->messageId; // reference the message id here. auto generate if not available
67
            $arr = [
68
                'is_success' => $data->status == 'Success',
69
                'message_id' => $data->messageId,
70
                'number'     => $data->number,
71
                'cost'       => $data->cost,
72
                'status'     => $data->status,
73
                'statusCode' => $data->statusCode,
74
            ];
75
76
            $this->data = (object) $arr;
77
78
            return $this;
79
        } else {
80
            // sms sending failed  // problem with gateway
81
            $arr = $result;
82
            $this->data = (object) $arr;
83
84
            return $this;
85
        }
86
    }
87
88
    /**
89
     * initialize the is_success parameter.
90
     *
91
     * @return bool
92
     */
93
    public function is_successful(): bool
94
    {
95
        return $this->is_success;
96
    }
97
98
    /**
99
     * assign the message ID as received on the response,auto generate if not available.
100
     *
101
     * @return mixed
102
     */
103
    public function getMessageID()
104
    {
105
        return $this->message_id;
106
    }
107
108
    /**
109
     * auto generate if not available.
110
     */
111
    public function getBalance(): float
112
    {
113
        $AT = new SMSGateway($this->settings->username, $this->settings->api_key);
114
        $application = $AT->application();
115
        $balance = $application->fetchApplicationData()['data']->UserData->balance;
116
        $replacements = ['/\bKES\b/', '/\bUGX\b/', '/\TSH\b/'];
117
118
        return (float) str_replace(' ', '', preg_replace($replacements, '', $balance));
119
    }
120
121
    /**
122
     * @param Request $request
123
     *
124
     * @return object
125
     */
126
    public function getDeliveryReportS(Request $request)
127
    {
128
        $status = $request->status;
129
130
        if ($status == 'Failed' || $status == 'Rejected') {
131
            $fs = $request->failureReason;
132
        } else {
133
            $fs = $status;
134
        }
135
136
        $data = [
137
            'status'       => $fs,
138
            'message_id'   => $request->id,
139
            'phone_number' => '',
140
        ];
141
142
        return (object) $data;
143
    }
144
}
145