Passed
Push — main ( 1b0804...b2ad10 )
by Hooman
12:11 queued 13s
created

Driver   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 162
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A message() 0 5 2
A getWebServiceResponce() 0 3 1
A getResult() 0 3 1
A getMessage() 0 7 2
A failedConnectToProvider() 0 10 1
A send() 0 10 2
A to() 0 5 1
A getRecipient() 0 3 1
A getSenderNumber() 0 3 1
1
<?php
2
3
namespace HoomanMirghasemi\Sms\Abstracts;
4
5
use HoomanMirghasemi\Sms\Contracts\Driver as DriverContract;
6
use HoomanMirghasemi\Sms\Contracts\Message;
7
use HoomanMirghasemi\Sms\Events\ProviderConnectionFailedEvent;
8
use HoomanMirghasemi\Sms\Events\SmsSentEvent;
9
use Illuminate\Support\Facades\Event;
10
11
abstract class Driver implements DriverContract
12
{
13
    /**
14
     * If it can not connect to provider this get false value.
15
     *
16
     * @var bool
17
     */
18
    protected bool $serviceActive = true;
19
20
    /**
21
     * Recipient (mobile number in E164 format).
22
     *
23
     * @param string
24
     */
25
    protected string $recipient;
26
27
    /**
28
     * Sender number.
29
     *
30
     * @param string
31
     */
32
    protected string $from;
33
34
    /**
35
     * Message.
36
     *
37
     * @var Message|string
38
     */
39
    protected Message|string $message;
40
41
    /**
42
     * The response of driver webservice of sending message.
43
     *
44
     * @var string
45
     */
46
    protected string $webserviceResponse;
47
48
    /**
49
     * Result of sending message.
50
     *
51
     * @var bool
52
     */
53
    protected bool|null $success = null;
54
55
    /**
56
     * Add recipient (phone or mobile numbers).
57
     *
58
     * @param string $recipient
59
     *
60
     * @return self
61
     */
62
    public function to(string $recipient): self
63
    {
64
        $this->recipient = $recipient;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Get recipient mobile number.
71
     *
72
     * @return string
73
     */
74
    public function getRecipient(): string
75
    {
76
        return $this->recipient;
77
    }
78
79
    /**
80
     * Get sender number (from attribute).
81
     *
82
     * @return string
83
     */
84
    public function getSenderNumber(): string
85
    {
86
        return $this->from;
87
    }
88
89
    /**
90
     * Set related message.
91
     *
92
     * @param Message $message
93
     *
94
     * @return self
95
     */
96
    public function message(Message|string $message): self
97
    {
98
        $this->message = $message instanceof Message ? $message : new \HoomanMirghasemi\Sms\Message($message);
99
100
        return $this;
101
    }
102
103
    /**
104
     * Get message in string.
105
     *
106
     * @return string
107
     */
108
    public function getMessage(): string
109
    {
110
        if ($this->message instanceof Message) {
111
            return $this->message->toString();
112
        }
113
114
        return $this->message;
115
    }
116
117
    /**
118
     * Get result of sending message is successful or not.
119
     *
120
     * @return bool
121
     */
122
    public function getResult(): bool
123
    {
124
        return $this->success;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->success could return the type null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
125
    }
126
127
    /**
128
     * Get webservice response.
129
     *
130
     * @return string
131
     */
132
    public function getWebServiceResponce(): string
133
    {
134
        return $this->webserviceResponse;
135
    }
136
137
    /**
138
     * Each driver should call this parent method at end of own send.
139
     * This fire SmsSentEvent.
140
     *
141
     * @see SmsSentEvent
142
     *
143
     * @return bool
144
     */
145
    public function send(): bool
146
    {
147
        if ($this->success === null) {
148
            throw new \BadMethodCallException('Abstract driver send method should only call in end of drivers with result of send');
149
        }
150
151
        $smsSentEvent = new SmsSentEvent($this);
152
        Event::dispatch($smsSentEvent);
153
154
        return $this->success;
155
    }
156
157
    /**
158
     * When a driver can not connect to provider this method called.
159
     * Also ProviderConnectionFailedEvent fired.
160
     *
161
     * @see ProviderConnectionFailedEvent
162
     */
163
    public function failedConnectToProvider()
164
    {
165
        $reflect = new \ReflectionClass($this);
166
        $providerName = strtolower($reflect->getShortName());
167
168
        $this->webserviceResponse = "System can not connect to {$providerName} webservice.";
169
        $this->success = false;
170
171
        $providerConnectionFailedEvent = new ProviderConnectionFailedEvent($this);
172
        Event::dispatch($providerConnectionFailedEvent);
173
    }
174
}
175