Skip to content

Commit

Permalink
Add the driver option for the sender
Browse files Browse the repository at this point in the history
Phpdoc improvements
  • Loading branch information
alexeygeno committed Aug 9, 2023
1 parent 2baa569 commit da45f6d
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 52 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ composer require jenssegers/mongodb laravel-notification-channels/twilio
'driver' => 'mongodb',
],
'sender' => [
'driver' => 'twilio',
'channel' => \NotificationChannels\Twilio\TwilioChannel::class
]
]
Expand All @@ -95,7 +96,7 @@ If the available options are not sufficient, you can redefine the service provid
```php
[
'storage' => [
'driver' => 'redis', // redis || mongodb
'driver' => 'redis', // 'redis' || 'mongodb'
'redis' => [
'connection' => 'default',
// the key settings - normally you don't need to change them
Expand All @@ -115,12 +116,8 @@ If the available options are not sufficient, you can redefine the service provid
],
],
'sender' => [
/**
* \Illuminate\Notifications\Channels\VonageSmsChannel::class || \NotificationChannels\Twilio\TwilioChannel::class || \NotificationChannels\Messagebird\MessagebirdChannel::class
*
* and many more https://github.com/laravel-notification-channels
*/
'channel' => \Illuminate\Notifications\Channels\VonageSmsChannel::class,
'driver' => 'vonage', // 'vonage' || 'twilio' || 'messagebird' and many more https://github.com/laravel-notification-channels
'channel' => \Illuminate\Notifications\Channels\VonageSmsChannel::class, // \Illuminate\Notifications\Channels\VonageSmsChannel::class || \NotificationChannels\Twilio\TwilioChannel::class || \NotificationChannels\Messagebird\MessagebirdChannel::class and many more https://github.com/laravel-notification-channels
'to_log' => false, // if enabled: instead of sending a real notification, debug it to the app log
],
'routes' => true, // managing the availability of the package routes without redefining the service provider
Expand Down
10 changes: 3 additions & 7 deletions config/phone-verification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

return [
'storage' => [
'driver' => 'redis', // redis || mongodb
'driver' => 'redis', // 'redis' || 'mongodb'
'redis' => [
'connection' => 'default',
// the key settings - normally you don't need to change them
Expand All @@ -22,12 +22,8 @@
],
],
'sender' => [
/**
* \Illuminate\Notifications\Channels\VonageSmsChannel::class || \NotificationChannels\Twilio\TwilioChannel::class || \NotificationChannels\Messagebird\MessagebirdChannel::class
*
* and many more https://github.com/laravel-notification-channels
*/
'channel' => \Illuminate\Notifications\Channels\VonageSmsChannel::class,
'driver' => 'vonage', // 'vonage' || 'twilio' || 'messagebird' and many more https://github.com/laravel-notification-channels
'channel' => \Illuminate\Notifications\Channels\VonageSmsChannel::class, // \Illuminate\Notifications\Channels\VonageSmsChannel::class || \NotificationChannels\Twilio\TwilioChannel::class || \NotificationChannels\Messagebird\MessagebirdChannel::class and many more https://github.com/laravel-notification-channels
'to_log' => false, // if enabled: instead of sending a real notification, debug it to the app log
],
'routes' => true, // managing the availability of the package routes without redefining the service provider
Expand Down
4 changes: 2 additions & 2 deletions src/Facades/PhoneVerification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Illuminate\Support\Facades\Facade;

/**
* @method static \AlexGeno\PhoneVerification\Manager initiate(string $to)
* @method static \AlexGeno\PhoneVerification\Manager complete(string $to, int $otp)
* @method static \AlexGeno\PhoneVerification\Manager\Initiator initiate(string $to)
* @method static \AlexGeno\PhoneVerification\Manager\Completer complete(string $to, int $otp)
*/
class PhoneVerification extends Facade
{
Expand Down
12 changes: 10 additions & 2 deletions src/Notifications/Otp.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
namespace AlexGeno\PhoneVerificationLaravel\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Support\Str;

class Otp extends Notification
{
protected string $content;

protected string $channel;

public function __construct(string $channel)
{
$this->channel = $channel;
}

/**
* Set content for the OTP notification.
*
Expand All @@ -27,7 +35,7 @@ public function content(string $content)
*/
public function via(object $notifiable)
{
return array_keys($notifiable->routes);
return [$this->channel];
}

/**
Expand All @@ -39,7 +47,7 @@ public function via(object $notifiable)
*/
public function __call(string $name, array $args)
{
if (str_starts_with($name, 'to') and count($args) == 1 and is_object(current($args))) {
if (Str::of($name)->startsWith('to') and count($args) == 1 and is_object(current($args))) {
return $this->content;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/PhoneVerification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
class PhoneVerification
{
/**
* Initiate phone verification process.
* Initiate verification process.
*
* @return mixed
* @return \AlexGeno\PhoneVerification\Manager\Initiator
*/
public function initiate(string $to)
{
return app(\AlexGeno\PhoneVerification\Manager\Initiator::class)->initiate($to);
}

/**
* Complete phone verification process.
* Complete verification process.
*
* @return \AlexGeno\PhoneVerification\Manager\Completer
*/
Expand Down
14 changes: 13 additions & 1 deletion src/PhoneVerificationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use AlexGeno\PhoneVerification\Storage\I as IStorage;
use AlexGeno\PhoneVerificationLaravel\Commands\Complete;
use AlexGeno\PhoneVerificationLaravel\Commands\Initiate;
use AlexGeno\PhoneVerificationLaravel\Notifications\Otp;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\ServiceProvider;
Expand All @@ -23,6 +24,7 @@ public function register()
$this->registerConfig();
$this->registerStorage();
$this->registerSender();
$this->registerOtp();
$this->registerManager();
$this->registerPhoneVerification();
}
Expand Down Expand Up @@ -101,13 +103,23 @@ protected function registerStorage()
*/
protected function registerSender()
{
$this->app->when(Sender::class)->needs('$channel')->giveConfig('phone-verification.sender.channel');
$this->app->when(Sender::class)->needs('$driver')->giveConfig('phone-verification.sender.driver');
$this->app->when(Sender::class)->needs('$toLog')->giveConfig('phone-verification.sender.to_log');
$this->app->bind(ISender::class, function ($container) {
return $container->make(Sender::class);
});
}

/**
* Register Otp using config settings.
*
* @return void
*/
protected function registerOtp()
{
$this->app->when(Otp::class)->needs('$channel')->giveConfig('phone-verification.sender.channel');
}

/**
* Build a config for Manager.
*
Expand Down
12 changes: 6 additions & 6 deletions src/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ class Sender implements I
{
protected Otp $otp;

protected string $channel;
protected string $driver;

protected bool $toLog;

/**
* Sender constructor.
* Constructor.
*/
public function __construct(Otp $otp, string $channel, bool $toLog)
public function __construct(Otp $otp, string $driver, bool $toLog)
{
$this->otp = $otp;
$this->channel = $channel;
$this->driver = $driver;
$this->toLog = $toLog;
}

Expand All @@ -32,9 +32,9 @@ public function __construct(Otp $otp, string $channel, bool $toLog)
public function invoke(string $to, string $text)
{
if ($this->toLog) {
\Illuminate\Support\Facades\Log::info("Pretended notification sending to {$this->channel}:{$to} with message: {$text}");
\Illuminate\Support\Facades\Log::info("Pretended notification sending to {$this->driver}:{$to} with message: {$text}");
} else {
Notification::route($this->channel, $to)->notify($this->otp->content($text));
Notification::route($this->driver, $to)->notify($this->otp->content($text));
}
}
}
2 changes: 1 addition & 1 deletion tests/Feature/CommndsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CommandsTest extends FeatureTestCase
use PHPMock;

/**
* Test verification process using artisan commands
* Test verification process using artisan commands.
*
* @runInSeparateProcess
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/CustomSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CustomSenderTest extends FeatureTestCase
protected string $serviceProvider = CustomSenderServiceProvider::class;

/**
* If a custom sender is resolved properly for the sender interface
* If a custom sender is resolved properly for the sender interface.
*
* @return void
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/CustomStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CustomStorageTest extends FeatureTestCase
protected string $serviceProvider = CustomStorageServiceProvider::class;

/**
* If a custom storage is resolved properly for the storage interface
* If a custom storage is resolved properly for the storage interface.
*
* @return void
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/FacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class FacadeTest extends FeatureTestCase
use PHPMock;

/**
* Test verification process using the facade
* Test verification process using the facade.
*
* @runInSeparateProcess
*
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/IgnoreRoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected function getEnvironmentSetUp($app)
}

/**
* If the route phone-verification/initiate is not available
* If the 'phone-verification/initiate' route is not available.
*
* @return void
*/
Expand All @@ -25,7 +25,7 @@ public function test_initiation_not_available()
}

/**
* If the route phone-verification/complete is not available
* If the 'phone-verification/complete' route is not available.
*
* @return void
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Providers/CustomSenderServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class CustomSenderServiceProvider extends PhoneVerificationServiceProvider
{
/**
* Register Foo as a sender
* Register Foo as a sender.
*
* return void
*/
Expand Down
14 changes: 7 additions & 7 deletions tests/Feature/UseRoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class UseRoutesTest extends FeatureTestCase
protected const LANG_MESSAGES = 'phone-verification::messages.';

/**
* Test the route phone-verification/initiate
* Test the 'phone-verification/initiate' route.
*
* @return void
*/
Expand All @@ -25,7 +25,7 @@ public function test_initiation_ok()
}

/**
* Test the initiation rate limit
* Test the initiation rate limit.
*
* @return void
*/
Expand All @@ -40,7 +40,7 @@ public function test_initiation_rate_limit_exceeded()
}

/**
* Test verification process using the routes
* Test verification process using the routes.
*
* @runInSeparateProcess
*
Expand Down Expand Up @@ -69,7 +69,7 @@ public function test_process_ok()
}

/**
* Test the completion rate limit
* Test the completion rate limit.
*
* @return void
*/
Expand All @@ -84,7 +84,7 @@ public function test_process_rate_limit_exceeded()
}

/**
* Test verification process using the routes when OTP is incorrect
* Test verification process using the routes when OTP is incorrect.
*
* @return void
*/
Expand All @@ -104,13 +104,13 @@ public function test_completion_otp_incorrect()
}

/**
* Test verification process using the routes when OTP is expired
* Test verification process using the routes when OTP is expired.
*
* @return void
*/
public function test_completion_otp_expired()
{
// No initiation has the same behaviour as the initiation expiration - the key ain a storage just doesn't exist
// No initiation has the same behavior as the initiation expiration - the key just doesn't exist in a storage
$expirationPeriodSecs = config('phone-verification.manager.rate_limits.complete.period_secs');

$response = $this->postJson('/phone-verification/complete', ['to' => '+15417543010', 'otp' => 0]);
Expand Down
24 changes: 14 additions & 10 deletions tests/Unit/SendersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
use AlexGeno\PhoneVerificationLaravel\Notifications\Otp;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;

class SendersTest extends UnitTestCase
{
/**
* Senders data provider.
*
* @return array<int, array<int, string>>
*/
public function channels()
public function senders()
{
return [
['messagebird', \NotificationChannels\Messagebird\MessagebirdChannel::class],
Expand All @@ -22,32 +25,33 @@ public function channels()
}

/**
* If notification facade was called properly for different channels
* If the notification facade was called properly for different channels.
*
* @see https://laravel.com/docs/9.x/mocking#on-demand-notifications
*
* @dataProvider channels
* @dataProvider senders
*
* @return void
*/
public function test_notification_invocation_ok(string $channel, string $class)
public function test_notification_invocation_ok(string $driver, string $channel)
{
$text = 'Test text';
$to = '+15417543010';
config(['phone-verification.sender.channel' => $class]);
config(['phone-verification.sender.channel' => $channel,
'phone-verification.sender.driver' => $driver]);

Notification::fake();

app(ISender::class)->invoke($to, $text);

Notification::assertSentOnDemand(Otp::class,
function ($notification, array $channels, AnonymousNotifiable $notifiable) use ($text, $to, $channel, $class) {
$toMethod = 'to'.ucfirst($channel);
function (Otp $notification, array $channels, AnonymousNotifiable $notifiable) use ($text, $to, $driver, $channel) {
$toMethod = 'to'.Str::ucfirst($driver);

return count($channels) == 1 && current($channels) === $class &&
return count($channels) == 1 && current($channels) === $channel &&
$notification->$toMethod($notifiable) == $text &&
$notification->via($notifiable) == [$class] &&
$notifiable->routeNotificationFor($class) == $to;
$notification->via($notifiable) == [$channel] &&
$notifiable->routeNotificationFor($driver) == $to;
}
);
}
Expand Down

0 comments on commit da45f6d

Please sign in to comment.