laravel-notification-channels /
vodafone
| 1 | <?php |
||
| 2 | |||
| 3 | namespace NotificationChannels\Vodafone; |
||
| 4 | |||
| 5 | use GuzzleHttp\Client; |
||
| 6 | use GuzzleHttp\Exception\GuzzleException; |
||
| 7 | use NotificationChannels\Vodafone\Exceptions\CouldNotReceiveNotification; |
||
| 8 | use NotificationChannels\Vodafone\Exceptions\CouldNotSendNotification; |
||
| 9 | use NotificationChannels\Vodafone\Exceptions\StandardNotification; |
||
| 10 | |||
| 11 | class VodafoneClient |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var string Vodafone's send API endpoint |
||
| 15 | */ |
||
| 16 | protected string $sendEndpoint = 'https://www.smsertech.com/apisend'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string Vodafone's status API endpoint |
||
| 20 | */ |
||
| 21 | protected string $statusEndpoint = 'https://www.smsertech.com/apistatus'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string Vodafone's receive API endpoint |
||
| 25 | */ |
||
| 26 | protected string $receiveEndpoint = 'https://www.smsertech.com/apiget'; |
||
| 27 | |||
| 28 | /** @var string Vodafone SMS username */ |
||
| 29 | protected $username; |
||
| 30 | |||
| 31 | /** @var string Vodafone SMS password */ |
||
| 32 | protected $password; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * VodafoneClient constructor. |
||
| 36 | * |
||
| 37 | * @param $username |
||
| 38 | * @param $password |
||
| 39 | */ |
||
| 40 | public function __construct() |
||
| 41 | { |
||
| 42 | $this->username = config('services.vodafone.username'); |
||
| 43 | $this->password = config('services.vodafone.password'); |
||
| 44 | |||
| 45 | return $this; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * VodafoneClient send method. |
||
| 50 | * |
||
| 51 | * @param $from |
||
| 52 | * @param $to |
||
| 53 | * @param $message |
||
| 54 | * |
||
| 55 | * @throws CouldNotSendNotification |
||
| 56 | * @throws StandardNotification |
||
| 57 | * @throws GuzzleException |
||
| 58 | * |
||
| 59 | * @return mixed Vodafone API result |
||
| 60 | */ |
||
| 61 | public function send($from, $to, $message) |
||
| 62 | { |
||
| 63 | $client = new Client(); |
||
| 64 | $res = $client->post($this->sendEndpoint, [ |
||
| 65 | 'form_params' => [ |
||
| 66 | 'username' => $this->username, |
||
| 67 | 'password' => $this->password, |
||
| 68 | 'to' => $to, |
||
| 69 | 'message' => $message, |
||
| 70 | 'from' => $from, |
||
| 71 | 'format' => 'json', |
||
| 72 | 'flash' => 0, |
||
| 73 | ], |
||
| 74 | ]); |
||
| 75 | |||
| 76 | if (!$res) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 77 | throw CouldNotSendNotification::serviceUnknownResponse(); |
||
| 78 | } |
||
| 79 | |||
| 80 | $body = $this->parseResponse($res); |
||
| 81 | |||
| 82 | if ($body->status === 'ERROR') { |
||
| 83 | throw StandardNotification::serviceRespondedWithAnError($body); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $body; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * VodafoneClient receive method. |
||
| 91 | * |
||
| 92 | * @throws CouldNotReceiveNotification |
||
| 93 | * @throws StandardNotification |
||
| 94 | * @throws GuzzleException |
||
| 95 | * |
||
| 96 | * @return mixed Vodafone API result |
||
| 97 | */ |
||
| 98 | public function receive() |
||
| 99 | { |
||
| 100 | $client = new Client(); |
||
| 101 | $res = $client->post($this->receiveEndpoint, [ |
||
| 102 | 'form_params' => [ |
||
| 103 | 'username' => $this->username, |
||
| 104 | 'password' => $this->password, |
||
| 105 | 'format' => 'json', |
||
| 106 | ], |
||
| 107 | ]); |
||
| 108 | |||
| 109 | if (!$res) { |
||
|
0 ignored issues
–
show
|
|||
| 110 | throw CouldNotReceiveNotification::serviceUnknownResponse(); |
||
| 111 | } |
||
| 112 | |||
| 113 | $body = $this->parseResponse($res); |
||
| 114 | |||
| 115 | if ($body->status === 'ERROR') { |
||
| 116 | if($body->code === 201) { |
||
| 117 | throw CouldNotReceiveNotification::noNewMessages(); |
||
| 118 | } |
||
| 119 | |||
| 120 | throw StandardNotification::serviceRespondedWithAnError($body); |
||
| 121 | } |
||
| 122 | |||
| 123 | return $body; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @throws CouldNotReceiveNotification |
||
| 128 | * @throws GuzzleException |
||
| 129 | * @throws StandardNotification |
||
| 130 | * |
||
| 131 | * @return mixed|null |
||
| 132 | */ |
||
| 133 | public static function getUnread() |
||
| 134 | { |
||
| 135 | return (new self())->receive(); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param $res |
||
| 140 | * |
||
| 141 | * @return mixed|null |
||
| 142 | */ |
||
| 143 | public function parseResponse($res) |
||
| 144 | { |
||
| 145 | return json_decode($res->getBody()->getContents())[0] ?? null; |
||
| 146 | } |
||
| 147 | } |
||
| 148 |