Skip to content

Commit

Permalink
Merge pull request #10 from IPeCompany/dev
Browse files Browse the repository at this point in the history
Add Notification Channel
  • Loading branch information
cryptommer authored Jul 22, 2024
2 parents 02f9de9 + 880a1aa commit 85755b3
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ add this line to the beginning of any class that you want to use smsir functions
```php
use Cryptommer\Smsir\Smsir;
```
#### Notification
add this function to your model
```php
public function routeNotificationForSmsir() {
return $this->phone_number
}
```
modify these lines in notification class
```php
public function via() {
return [\Cryptommer\Smsir\Notifications\SmsirChannel::class] // or 'smsir'
}
```
add this function to notification class
```php
public function toSmsir(object $notifiable) {
/**
* template_id string
* parameters array of key and value that key equal to the key in template id
*/
return new \Cryptommer\Smsir\Notifications\SmsirMessage()
->template_id($template_id)
->parameters($parameters)
}
```
### Pure PHP
```php
require __DIR__ . '/vendor/autoload.php';
Expand Down
36 changes: 36 additions & 0 deletions src/Notifications/SmsirChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Cryptommer\Smsir\Notifications;

use Cryptommer\Smsir\Classes\Smsir;
use Illuminate\Notifications\Notification;

class SmsirChannel
{

/**
* @var Smsir
*/
protected $client;

public function __construct(Smsir $client) {
$this->client = $client;
}

public function send($notifiable, Notification $notification)
{
if (! $to = $notifiable->routeNotificationFor('smsir', $notification)) {
return;
}

if (! empty($this->to)) {
return;
}

$message = $notification->toSmsir($notifiable);

$this->client->send()->verify($to, $message->template_id, $message->parameters);
}


}
27 changes: 27 additions & 0 deletions src/Notifications/SmsirMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Cryptommer\Smsir\Notifications;

class SmsirMessage
{

public $template_id;

public $parameters;

public function __construct($template_id = null, $parameters = null) {
$this->template_id = $template_id;
$this->parameters = $parameters;
}

public function template_id($template_id) {
$this->template_id = $template_id;
return $this;
}

public function parameters(array $parameters) {
$this->parameters = $parameters;
return $this;
}

}
15 changes: 15 additions & 0 deletions src/SmsirServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Cryptommer\Smsir;

use Cryptommer\Smsir\Classes\Smsir;
use Cryptommer\Smsir\Notifications\SmsirChannel;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\ServiceProvider;

class SmsirServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -59,5 +62,17 @@ public function register()
$this->app->singleton('Smsir', function () {
return new Smsir();
});

$this->app->bind(SmsirChannel::class, function ($app) {
return new SmsirChannel(
$this->app->make(Smsir::class)
);
});

Notification::resolved(function (ChannelManager $service) {
$service->extend('smsir', function ($app) {
return $this->app->make(SmsirChannel::class);
});
});
}
}

0 comments on commit 85755b3

Please sign in to comment.