Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
GautierDele committed Mar 20, 2024
0 parents commit 280034b
Show file tree
Hide file tree
Showing 11 changed files with 560 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor
composer.lock
/phpunit.xml
.phpunit.result.cache
.idea
1 change: 1 addition & 0 deletions .phpunit.cache/test-results
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"Xefi\\SmsFactor\\Tests\\Unit\\Channels\\SmsFactorSmsChannelTest::testSmsIsSentViaSmsFactor":8},"times":{"Xefi\\SmsFactor\\Tests\\Unit\\Channels\\SmsFactorSmsChannelTest::testSmsIsSentViaSmsFactor":0.04,"Xefi\\SmsFactor\\Tests\\Unit\\Channels\\SmsFactorSmsChannelTest::testSmsIsSentViaSmsFactorWithCustomSender":0,"Xefi\\SmsFactor\\Tests\\Unit\\Channels\\SmsFactorSmsChannelTest::testSmsIsSentViaSmsFactorWithCustomDelay":0,"Xefi\\SmsFactor\\Tests\\Unit\\Channels\\SmsFactorSmsChannelTest::testSmsIsSentViaSmsFactorWithCustomPushType":0,"Xefi\\SmsFactor\\Tests\\Unit\\Channels\\SmsFactorSmsChannelTest::testSmsIsSentViaSmsFactorWithCustomGsmsmsid":0}}
4 changes: 4 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
php:
preset: laravel
js: true
css: true
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Laravel SMSFactor Notification Channel

## Prerequisites

Before you can send notifications via SMSFactor, you need to install the `xefi/sms-factor-notification-channel`

```bash
composer require xefi/sms-factor-notification-channel
```

The package includes a [configuration file](https://github.com/xefi/sms-factor-notification-channel/config/sms-factor.php). However, you are not required to export this configuration file to your own application. You can simply use the `SMS_FACTOR_TOKEN` environment variables to define your SMSFactor token.

```
SMS_FACTOR_TOKEN=your-token
```

## Formating SMS Notifications

If a notification supports being sent as an SMS, you should define a `toSmsFactor` method on the notification class. This method will receive a $notifiable entity and should return an `Xefi\SmsFactor\Messages\SmsFactorMessage` instance:

```php
use Xefi\SmsFactor\Messages\SmsFactorMessage;

/**
* Get the SMSFactor representation of the notification.
*/
public function toSmsFactor(object $notifiable): SmsFactorMessage
{
return (new SmsFactorMessage)
->text('Your SMS message content');
}
```

## Customizing the "To" Number

If you would like to customize the number depending on the notifiable object you are calling, you'll need to implement the `routeNotificationForSmsFactor` method on your notifiable model:

```php
use Illuminate\Notifications\Notification;

/**
* Get the corresponding phone number for the current model.
*/
public function routeNotificationForSmsFactor(Notification $notification)
{
return $this->phone;
}
```

## Customizing the "From" Number

If you would like to send some notifications from a phone number that is different from the phone number specified by your `SMS_FACTOR_SMS_FROM` environment variable, you may call the `sender` method on a `SmsFactorMessage` instance:

```php
use Xefi\SmsFactor\Messages\SmsFactorMessage;

/**
* Get the SMSFactor representation of the notification.
*/
public function toSmsFactor(object $notifiable): SmsFactorMessage
{
return (new SmsFactorMessage)
->content('Your SMS message content')
->sender('15554443333');
}
```
53 changes: 53 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "xefi/sms-factor-notification-channel",
"description": "Sms Factor Notification Channel for laravel.",
"keywords": ["laravel", "notifications", "smsfactor"],
"license": "MIT",
"authors": [
{
"name": "Gautier Deleglise",
"email": "[email protected]"
}
],
"require": {
"php": "^8.0",
"illuminate/notifications": "^8.0|^9.0|^10.0|^11.0",
"illuminate/support": "^8.0|^9.0|^10.0|^11.0",
"smsfactor/smsfactor-php-sdk": "^1.0.5"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.2",
"mockery/mockery": "^1.0",
"orchestra/testbench": "^6.0|^7.0|^8.0|^9.0",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.0|^10.4"
},
"autoload": {
"psr-4": {
"Xefi\\SmsFactor\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Xefi\\SmsFactor\\Tests\\": "tests/"
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true
}
},
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
},
"laravel": {
"providers": [
"Xefi\\SmsFactor\\SmsFactorChannelServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
28 changes: 28 additions & 0 deletions config/sms-factor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| SMS "From" Number
|--------------------------------------------------------------------------
|
| This configuration option defines the phone number that will be used as
| the "from" number for all outgoing text messages.
| See: https://dev.smsfactor.com/en/api/sms/getting-started
|
*/

'sms_from' => env('SMS_FACTOR_SMS_FROM'),

/*
|--------------------------------------------------------------------------
| API Token
|--------------------------------------------------------------------------
|
| This configuration contain your API token, which may be accessed
| from your SMSFactor dashboard.
|
*/

'api_token' => env('SMS_FACTOR_TOKEN'),
];
8 changes: 8 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" beStrictAboutTestsThatDoNotTestAnything="true" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnError="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
</phpunit>
39 changes: 39 additions & 0 deletions src/Channels/SmsFactorSmsChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Xefi\SmsFactor\Channels;

use Xefi\SmsFactor\Messages\SmsFactorMessage;
use SMSFactor\Message;
use Illuminate\Notifications\Notification;

class SmsFactorSmsChannel extends Notification
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return \SmsFactor\ApiResponse
*/
public function send($notifiable, Notification $notification)
{
if (! $to = $notifiable->routeNotificationFor('sms-factor', $notification)) {
return;
}

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

if (is_string($message)) {
$message = (new SmsFactorMessage())->text($message);
}

return Message::send([
'to' => $to,
'text' => trim($message->text),
'delay' => $message->delay,
'pushtype' => $message->pushtype ?? null,
'sender' => $message->sender ?? config('sms-factor.sms_from'),
'gsmsmsid' => $message->gsmsmsid ?? null,
]);
}
}
112 changes: 112 additions & 0 deletions src/Messages/SmsFactorMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Xefi\SmsFactor\Messages;

use Carbon\Carbon;

class SmsFactorMessage
{
/**
* The message text.
*
* @var string
*/
public $text;

/**
* The push type (alert or marketing).
*
* @var string
*/
public $pushtype;

/**
* The date that the sms should be sent at (optional).
*
* @var \Carbon\Carbon
*/
public $delay;

/**
* The sender that should be used.
*
* @var string
*/
public $sender;

/**
* An id of your choice to link it to its delivery report.
*
* @var string
*/
public $gsmsmsid;

/**
* Set the message text.
*
* @param string $text
* @return $this
*/
public function text($text)
{
$this->text = $text;

return $this;
}

/**
* Set the message pushtype.
*
* @param string $pushtype
* @return $this
*/
public function pushtype($pushtype)
{
$this->pushtype = $pushtype;

return $this;
}

/**
* Set the message delay.
*
* @param \Carbon\Carbon $delay
* @return $this
*/
public function delay($delay)
{
if ($delay instanceof Carbon) {
$delay = $delay->format('Y-m-d H:i:s');
}

$this->delay = $delay;

return $this;
}

/**
* Set the message sender.
*
* @param string $sender
* @return $this
*/
public function sender($sender)
{
$this->sender = $sender;

return $this;
}

/**
* Set the message gsmsmsid.
*
* @param string $gsmsmsid
* @return $this
*/
public function gsmsmsid($gsmsmsid)
{
$this->gsmsmsid = $gsmsmsid;

return $this;
}
}
43 changes: 43 additions & 0 deletions src/SmsFactorChannelServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Xefi\SmsFactor;

use Illuminate\Support\ServiceProvider;
use Xefi\SmsFactor\Channels\SmsFactorSmsChannel;
use Illuminate\Support\Facades\Notification;
use Illuminate\Notifications\ChannelManager;

class SmsFactorChannelServiceProvider extends ServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/sms-factor.php', 'sms-factor');

Notification::resolved(function (ChannelManager $service) {
$service->extend('sms-factor', function ($app) {
return $app->make(SmsFactorSmsChannel::class);
});
});
}

/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
\SMSFactor\SMSFactor::setApiToken(config('sms-factor.api_token'));

if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/sms-factor.php' => $this->app->configPath('sms-factor.php'),
], 'sms-factor');
}
}
}
Loading

0 comments on commit 280034b

Please sign in to comment.