From dd9b8d0a4a22bd5b2510c596e9569876dc91fedf Mon Sep 17 00:00:00 2001 From: arya Date: Fri, 17 Nov 2023 15:56:50 +0330 Subject: [PATCH] [feat] implement feature to manually pass the driver This PR adds a new feature so that you can manually send a message with the selected driver instead of using the default value --- src/Chapaar.php | 26 ++++++++++++------- src/Contracts/DriverMessage.php | 4 +++ .../Farapayamak/FarapayamakMessage.php | 8 ++++++ src/Drivers/Ghasedak/GhasedakMessage.php | 8 ++++++ src/Drivers/Kavenegar/KavenegarMessage.php | 8 ++++++ src/Drivers/SmsIr/SmsIrMessage.php | 8 ++++++ src/SmsChannel.php | 20 +++++++++----- src/SmsMessage.php | 15 ++++------- 8 files changed, 70 insertions(+), 27 deletions(-) diff --git a/src/Chapaar.php b/src/Chapaar.php index 3ae5499..ea5c0a3 100755 --- a/src/Chapaar.php +++ b/src/Chapaar.php @@ -10,14 +10,9 @@ class Chapaar { protected DriverConnector $driver; - public function __construct() - { - $this->driver = $this->getDefaultDriver(); - } - public function getDefaultSetting(): object { - return $this->driver::setting(); + return $this->driver()::setting(); } public function getDefaultDriver(): DriverConnector @@ -29,21 +24,32 @@ public function getDefaultDriver(): DriverConnector public function send($message): object { - return $this->driver->send($message); + return $this->driver($message->driver)->send($message); } public function verify(DriverMessage $message): object { - return $this->driver->verify($message); + return $this->driver($message->getDriver())->verify($message); } public function account(): object { - return $this->driver->account(); + + return $this->driver()->account(); } public function outbox(int $page_size = 100, int $page_number = 1): object { - return $this->driver->outbox($page_size, $page_number); + return $this->driver()->outbox($page_size, $page_number); + } + + protected function driver(Drivers $driver = null): DriverConnector + { + $connector = $driver + ? Drivers::tryFrom($driver->value)->connector() + : Drivers::tryFrom(config('chapaar.default'))->connector(); + + return new $connector; + } } diff --git a/src/Contracts/DriverMessage.php b/src/Contracts/DriverMessage.php index df9fe81..3362f49 100644 --- a/src/Contracts/DriverMessage.php +++ b/src/Contracts/DriverMessage.php @@ -2,8 +2,12 @@ namespace TookanTech\Chapaar\Contracts; +use TookanTech\Chapaar\Enums\Drivers; + interface DriverMessage { + public function getDriver(): Drivers; + public function setFrom($from): self; public function getFrom(); diff --git a/src/Drivers/Farapayamak/FarapayamakMessage.php b/src/Drivers/Farapayamak/FarapayamakMessage.php index fb5162f..a8e95aa 100644 --- a/src/Drivers/Farapayamak/FarapayamakMessage.php +++ b/src/Drivers/Farapayamak/FarapayamakMessage.php @@ -3,9 +3,12 @@ namespace TookanTech\Chapaar\Drivers\Farapayamak; use TookanTech\Chapaar\Contracts\DriverMessage; +use TookanTech\Chapaar\Enums\Drivers; class FarapayamakMessage implements DriverMessage { + public Drivers $driver = Drivers::FARAPAYAMAK; + protected string $content = ''; protected string $from = ''; @@ -96,4 +99,9 @@ public function setFlash(bool $flash): void { $this->flash = $flash; } + + public function getDriver(): Drivers + { + return $this->driver; + } } diff --git a/src/Drivers/Ghasedak/GhasedakMessage.php b/src/Drivers/Ghasedak/GhasedakMessage.php index e9b56e3..758ce82 100644 --- a/src/Drivers/Ghasedak/GhasedakMessage.php +++ b/src/Drivers/Ghasedak/GhasedakMessage.php @@ -3,9 +3,12 @@ namespace TookanTech\Chapaar\Drivers\Ghasedak; use TookanTech\Chapaar\Contracts\DriverMessage; +use TookanTech\Chapaar\Enums\Drivers; class GhasedakMessage implements DriverMessage { + public Drivers $driver = Drivers::GHASEDAK; + protected string $content = ''; protected string $from = ''; @@ -124,4 +127,9 @@ public function setDate(string $date): void { $this->date = $date; } + + public function getDriver(): Drivers + { + return $this->driver; + } } diff --git a/src/Drivers/Kavenegar/KavenegarMessage.php b/src/Drivers/Kavenegar/KavenegarMessage.php index 4f71f0e..cbf378e 100644 --- a/src/Drivers/Kavenegar/KavenegarMessage.php +++ b/src/Drivers/Kavenegar/KavenegarMessage.php @@ -3,12 +3,15 @@ namespace TookanTech\Chapaar\Drivers\Kavenegar; use TookanTech\Chapaar\Contracts\DriverMessage; +use TookanTech\Chapaar\Enums\Drivers; /* * @method setTemplate */ class KavenegarMessage implements DriverMessage { + public Drivers $driver = Drivers::KAVENEGAR; + /** * The message type. */ @@ -126,4 +129,9 @@ public function setDate(string $date): self return $this; } + + public function getDriver(): Drivers + { + return $this->driver; + } } diff --git a/src/Drivers/SmsIr/SmsIrMessage.php b/src/Drivers/SmsIr/SmsIrMessage.php index 1322f4a..f997195 100644 --- a/src/Drivers/SmsIr/SmsIrMessage.php +++ b/src/Drivers/SmsIr/SmsIrMessage.php @@ -3,9 +3,12 @@ namespace TookanTech\Chapaar\Drivers\SmsIr; use TookanTech\Chapaar\Contracts\DriverMessage; +use TookanTech\Chapaar\Enums\Drivers; class SmsIrMessage implements DriverMessage { + public Drivers $driver = Drivers::SMSIR; + protected string $content = ''; protected string $from = ''; @@ -96,4 +99,9 @@ public function setDate(string $date): void { $this->date = $date; } + + public function getDriver(): Drivers + { + return $this->driver; + } } diff --git a/src/SmsChannel.php b/src/SmsChannel.php index ff5fcd7..766bfb0 100644 --- a/src/SmsChannel.php +++ b/src/SmsChannel.php @@ -4,6 +4,7 @@ use TookanTech\Chapaar\Contracts\DriverConnector; use TookanTech\Chapaar\Contracts\DriverMessage; +use TookanTech\Chapaar\Enums\Drivers; class SmsChannel { @@ -11,11 +12,6 @@ class SmsChannel protected DriverMessage $message; - public function __construct() - { - $this->driver = \TookanTech\Chapaar\Facades\Chapaar::getDefaultDriver(); - } - public function send($notifiable, $notification) { @@ -29,10 +25,20 @@ public function send($notifiable, $notification) return 0; } if ($template) { - return $this->driver->verify($message); + return $this->driver($message->getDriver())->verify($message); } else { - return $this->driver->send($message); + return $this->driver($message->getDriver())->send($message); } } + + protected function driver(Drivers $driver = null): DriverConnector + { + $connector = $driver + ? Drivers::tryFrom($driver->value)->connector() + : Drivers::tryFrom(config('chapaar.default'))->connector(); + + return new $connector; + + } } diff --git a/src/SmsMessage.php b/src/SmsMessage.php index 8c55a02..cee5b46 100644 --- a/src/SmsMessage.php +++ b/src/SmsMessage.php @@ -7,17 +7,12 @@ class SmsMessage { - protected string $message_driver; - - protected DriverMessage $driver; - - public function __construct() + public function driver($driver = null): DriverMessage { - $this->message_driver = Drivers::tryFrom(config('chapaar.default'))->message(); - } + $driver_message = $driver + ? Drivers::tryFrom($driver->value)->message() + : Drivers::tryFrom(config('chapaar.default'))->message(); - public function driver(): DriverMessage - { - return new $this->message_driver; + return new $driver_message; } }