diff --git a/.php_cs.dist.php b/.php_cs.dist.php index 75757bf..d82df72 100644 --- a/.php_cs.dist.php +++ b/.php_cs.dist.php @@ -46,7 +46,6 @@ ->exclude("vendor") ->in([ __DIR__ . '/src', - __DIR__ . '/tests', ]) ->name('*.php') ->notName('*.blade.php') diff --git a/config/resala.php b/config/resala.php index 8505fd4..af5051a 100644 --- a/config/resala.php +++ b/config/resala.php @@ -5,6 +5,7 @@ use RobustTools\Resala\Drivers\InfobipDriver; use RobustTools\Resala\Drivers\VodafoneDriver; use RobustTools\Resala\Drivers\VectoryLinkDriver; +use RobustTools\Resala\Drivers\BrandEncodeDriver; return [ @@ -66,6 +67,14 @@ 'sender_id' => env('GATEWAYSA_SENDER_ID'), 'templateid' => env('GATEWAYSA_TEMPLATE_ID'), ], + + 'brandencode' => [ + 'end_point' => env('BRANDENCODE_END_POINT'), + 'username' => env('BRANDENCODE_USERNAME'), + 'password' => env('BRANDENCODE_PASSWORD'), + 'sender_name' => env('BRANDENCODE_SENDER_NAME', 'Brandencode'), + 'lang' => env('BRANDENCODE_LANG', 'E') + ], ], /* @@ -81,6 +90,7 @@ 'connekio' => ConnekioDriver::class, 'infobip' => InfobipDriver::class, 'vectory_link' => VectoryLinkDriver::class, - 'gateway_sa' => GatewaySA::class + 'gateway_sa' => GatewaySA::class, + 'brandencode' => BrandEncodeDriver::class ], ]; diff --git a/src/Abstracts/Driver.php b/src/Abstracts/Driver.php index 1873068..348fcc4 100644 --- a/src/Abstracts/Driver.php +++ b/src/Abstracts/Driver.php @@ -1,4 +1,5 @@ argument('driver'); if (! array_key_exists($driver, config('resala.map'))) { - $this->error("provided driver does not exists, you may check available drivers: " . implode(", ", array_keys(config('resala.map')))); + $this->error( + "provided driver does not exists, you may check available drivers: " . + implode(", ", array_keys(config('resala.map'))) + ); return; } @@ -78,5 +82,9 @@ private function getStubContent(): string if ($this->argument('driver') == "vectory_link") { return File::get(__DIR__ . "/../../stubs/vectory_link.env.stub"); } + + if ($this->argument('driver') == "brandencode") { + return File::get(__DIR__ . "/../../stubs/brandencode.env.stub"); + } } } diff --git a/src/Contracts/SMSDriverInterface.php b/src/Contracts/SMSDriverInterface.php index 05c0db7..35913e4 100644 --- a/src/Contracts/SMSDriverInterface.php +++ b/src/Contracts/SMSDriverInterface.php @@ -1,4 +1,5 @@ username = $config["username"]; + $this->password = $config["password"]; + $this->senderName = $config["sender_name"]; + $this->endPoint = $config["end_point"]; + $this->lang = $config["lang"]; + } + + /** + * @param string|array $recipients + * @return string|array + */ + public function to($recipients) + { + return $this->recipients = $this->toMultiple($recipients) + ? implode(', ', $recipients) + : $recipients; + } + + public function message(string $message): string + { + return $this->message = $message; + } + + public function send(): SMSDriverResponseInterface + { + $response = HTTP::get($this->endPoint, $this->headers(), $this->payload()); + + return new BrandEncodeResponse($response); + } + + protected function payload(): array + { + return + [ + "message" => $this->message, + "receiver" => $this->recipients, + "sender" => $this->senderName, + 'language' => $this->lang, + 'username' => $this->username, + 'password' => $this->password + ]; + } + + protected function headers(): array + { + return [ + 'Content-Type' => 'application/json' + ]; + } +} diff --git a/src/Drivers/ConnekioDriver.php b/src/Drivers/ConnekioDriver.php index 94db516..3039749 100644 --- a/src/Drivers/ConnekioDriver.php +++ b/src/Drivers/ConnekioDriver.php @@ -1,8 +1,10 @@ $this->senderName ]; - $this->toMultiple($this->recipients) - ? $payload['mobile_list'] = array_map(fn ($recipient) => ['msisdn' => $this->formatPhoneNumber($recipient)], $this->recipients) - : $payload["msisdn"] = $this->formatPhoneNumber($this->recipients); + $this->toMultiple($this->recipients) ? $payload['mobile_list'] = array_map( + fn($recipient) => ['msisdn' => $this->formatPhoneNumber($recipient)], + $this->recipients + ) : $payload["msisdn"] = $this->formatPhoneNumber($this->recipients); return json_encode($payload); } @@ -76,7 +79,10 @@ protected function headers(): array return [ 'Content-Type' => 'application/json', 'Accept' => 'application/json', - 'Authorization' => sprintf("Basic %s", base64_encode($this->username . ':' . $this->password . ':' . $this->accountId)) + 'Authorization' => sprintf( + "Basic %s", + base64_encode($this->username . ':' . $this->password . ':' . $this->accountId) + ) ]; } @@ -92,7 +98,7 @@ protected function formatPhoneNumber($phoneNumber): string if (substr($phoneNumber, 0, 1) == '0') { $phoneNumber = '2' . $phoneNumber; } - + return $phoneNumber; } } diff --git a/src/Drivers/GatewaySA.php b/src/Drivers/GatewaySA.php index 690ac7f..7197aa2 100644 --- a/src/Drivers/GatewaySA.php +++ b/src/Drivers/GatewaySA.php @@ -1,8 +1,10 @@ 'application/json']; } -} \ No newline at end of file +} diff --git a/src/Drivers/InfobipDriver.php b/src/Drivers/InfobipDriver.php index 42cba3a..0e66cc3 100644 --- a/src/Drivers/InfobipDriver.php +++ b/src/Drivers/InfobipDriver.php @@ -1,8 +1,10 @@ toMultiple($this->recipients)) { foreach ($this->recipients as $recipient) { - $hashableKey .= sprintf("&SenderName=%s&ReceiverMSISDN=%s&SMSText=%s", $this->senderName, $recipient, $this->message); + $hashableKey .= sprintf( + "&SenderName=%s&ReceiverMSISDN=%s&SMSText=%s", + $this->senderName, + $recipient, + $this->message + ); } return $hashableKey; } else { - return sprintf("AccountId=%s&Password=%s&SenderName=%s&ReceiverMSISDN=%s&SMSText=%s", $this->accountId, $this->password, $this->senderName, $this->recipients, $this->message); + return sprintf( + "AccountId=%s&Password=%s&SenderName=%s&ReceiverMSISDN=%s&SMSText=%s", + $this->accountId, + $this->password, + $this->senderName, + $this->recipients, + $this->message + ); } } } diff --git a/src/Facades/SMS.php b/src/Facades/SMS.php index b381dbb..dc3ca64 100644 --- a/src/Facades/SMS.php +++ b/src/Facades/SMS.php @@ -1,4 +1,5 @@ response = json_decode($response->getBody()->getContents(), true); + $this->statusCode = $response->getStatusCode(); + } + + /** + * @inheritDoc + */ + public function success(): bool + { + return $this->statusCode >= 200 && $this->statusCode < 300 && $this->response['code'] == self::OK; + } + + /** + * @inheritDoc + */ + public function body(): string + { + return $this->response['message']; + } +} diff --git a/src/Response/ConnekioResponse.php b/src/Response/ConnekioResponse.php index 5991e60..751fbc8 100644 --- a/src/Response/ConnekioResponse.php +++ b/src/Response/ConnekioResponse.php @@ -1,4 +1,5 @@ driver->send(); } } - diff --git a/src/SMSServiceProvider.php b/src/SMSServiceProvider.php index 8e421b0..184ec65 100644 --- a/src/SMSServiceProvider.php +++ b/src/SMSServiceProvider.php @@ -1,4 +1,5 @@ app->singleton('sms', function () { - return new SMS; + return new SMS(); }); } } diff --git a/src/Support/Config.php b/src/Support/Config.php index 189442e..74d0925 100644 --- a/src/Support/Config.php +++ b/src/Support/Config.php @@ -1,15 +1,16 @@ config = new ConfigRepository( $this->configurations($filepath) diff --git a/src/Support/ConfigRepository.php b/src/Support/ConfigRepository.php index f4ded0e..18200f1 100644 --- a/src/Support/ConfigRepository.php +++ b/src/Support/ConfigRepository.php @@ -1,4 +1,5 @@ domDocument->saveXML(); } - private function rootElement() : DOMElement + private function rootElement(): DOMElement { return $this->domDocument->createElement('SubmitSMSRequest'); } - private function setRootElementAttributes(DOMElement $rootElement) : void + private function setRootElementAttributes(DOMElement $rootElement): void { $attr_xmlns = new DOMAttr('xmlns:', "http://www.edafa.com/web2sms/sms/model/"); $attr_xmlns_xsi = new DOMAttr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); - $attr_xsi_schemaLocation = new DOMAttr("xsi:schemaLocation", "http://www.edafa.com/web2sms/sms/model/ SMSAPI.xsd"); + + $attr_xsi_schemaLocation = new DOMAttr( + "xsi:schemaLocation", + "http://www.edafa.com/web2sms/sms/model/ SMSAPI.xsd" + ); + $attr_xsi_type = new DOMAttr("xsi:type", "SubmitSMSRequest"); $rootElement->setAttributeNode($attr_xmlns); @@ -77,22 +83,22 @@ private function setRootElementAttributes(DOMElement $rootElement) : void $rootElement->setAttributeNode($attr_xsi_type); } - private function generateAccountIdElement(DOMElement $rootElement) : void + private function generateAccountIdElement(DOMElement $rootElement): void { $rootElement->appendChild($this->domDocument->createElement('AccountId', $this->accountId)); } - private function generatePasswordElement(DOMElement $rootElement) : void + private function generatePasswordElement(DOMElement $rootElement): void { $rootElement->appendChild($this->domDocument->createElement('Password', $this->password)); } - private function generateSecureHashElement(DOMElement $rootElement) : void + private function generateSecureHashElement(DOMElement $rootElement): void { $rootElement->appendChild($this->domDocument->createElement('SecureHash', $this->secureHash)); } - private function generateSMSListElement(DOMElement $rootElement, string $recipient) : void + private function generateSMSListElement(DOMElement $rootElement, string $recipient): void { $sms_list_node = $rootElement->appendChild($this->domDocument->createElement("SMSList")); $sms_list_node->appendChild($this->domDocument->createElement("SenderName", $this->senderName)); diff --git a/stubs/brandencode.env.stub b/stubs/brandencode.env.stub new file mode 100644 index 0000000..c15a6f9 --- /dev/null +++ b/stubs/brandencode.env.stub @@ -0,0 +1,5 @@ +BRANDENCODE_USERNAME= +BRANDENCODE_PASSWORD= +BRANDENCODE_SENDER_NAME= +BRANDENCODE_END_POINT=https://sms.brandencode.com/api/send/sms/dlr +BRANDENCODE_LANG= \ No newline at end of file diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index dca2143..63a7aeb 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -1,4 +1,5 @@ assertTrue(true); }