Skip to content

Commit

Permalink
[feat] implement feature to manually pass the driver
Browse files Browse the repository at this point in the history
This PR adds a new feature so that you can manually send a message with the selected driver instead of using the default value
  • Loading branch information
aryala7 committed Nov 17, 2023
1 parent 1fa1d93 commit dd9b8d0
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 27 deletions.
26 changes: 16 additions & 10 deletions src/Chapaar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

}
}
4 changes: 4 additions & 0 deletions src/Contracts/DriverMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions src/Drivers/Farapayamak/FarapayamakMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -96,4 +99,9 @@ public function setFlash(bool $flash): void
{
$this->flash = $flash;
}

public function getDriver(): Drivers
{
return $this->driver;
}
}
8 changes: 8 additions & 0 deletions src/Drivers/Ghasedak/GhasedakMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -124,4 +127,9 @@ public function setDate(string $date): void
{
$this->date = $date;
}

public function getDriver(): Drivers
{
return $this->driver;
}
}
8 changes: 8 additions & 0 deletions src/Drivers/Kavenegar/KavenegarMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -126,4 +129,9 @@ public function setDate(string $date): self

return $this;
}

public function getDriver(): Drivers
{
return $this->driver;
}
}
8 changes: 8 additions & 0 deletions src/Drivers/SmsIr/SmsIrMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -96,4 +99,9 @@ public function setDate(string $date): void
{
$this->date = $date;
}

public function getDriver(): Drivers
{
return $this->driver;
}
}
20 changes: 13 additions & 7 deletions src/SmsChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@

use TookanTech\Chapaar\Contracts\DriverConnector;
use TookanTech\Chapaar\Contracts\DriverMessage;
use TookanTech\Chapaar\Enums\Drivers;

class SmsChannel
{
protected DriverConnector $driver;

protected DriverMessage $message;

public function __construct()
{
$this->driver = \TookanTech\Chapaar\Facades\Chapaar::getDefaultDriver();
}

public function send($notifiable, $notification)
{

Expand All @@ -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;

}
}
15 changes: 5 additions & 10 deletions src/SmsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit dd9b8d0

Please sign in to comment.