Skip to content

Commit

Permalink
feat: use v2 api client, add metadata (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
atymic authored Feb 17, 2024
1 parent 072afbb commit f39e903
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 42 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.3, 7.4, 8.0]
php: [8.2]

name: Tests on PHP ${{ matrix.php }} - ${{ matrix.stability }}

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
14 changes: 9 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
}
],
"require": {
"php": ">=7.2",
"illuminate/notifications": "~5.0 || ~6.0 || ~7.0 || ~8.0 || ~9.0 || ~10.0",
"illuminate/support": "~5.0 || ~6.0 || ~7.0 || ~8.0 || ~9.0 || ~10.0",
"touchsms/touchsms": "^1.3"
"php": ">=8.2",
"illuminate/notifications": "~8.0 || ~9.0 || ~10.0 || ~11.0",
"illuminate/support": "~8.0 || ~9.0 || ~10.0 || ~11.0",
"symfony/http-client": "^7.0",
"touchsms/touchsms": "^2.0"
},
"require-dev": {
"mockery/mockery": "^1.0",
Expand Down Expand Up @@ -43,6 +44,9 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": true
}
}
}
35 changes: 23 additions & 12 deletions src/TouchSmsChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

use Illuminate\Notifications\Notification;
use NotificationChannels\TouchSms\Exceptions\CouldNotSendNotification;
use TouchSMS\TouchSMS\touchSMS;
use TouchSms\ApiClient\Api\Model\OutboundMessage;
use TouchSms\ApiClient\Api\Model\SendMessageBody;
use TouchSms\ApiClient\Client;

class TouchSmsChannel
{
/** @var touchSMS */
private $client;
/** @var Client */
private Client $client;

public function __construct(touchSMS $client)
public function __construct(Client $client)
{
$this->client = $client;
}
Expand All @@ -38,15 +40,24 @@ public function send($notifiable, Notification $notification): void
return;
}

$response = $this->client->sendMessage(
$message->content,
$to,
$message->reference,
$message->sender ?? config('services.touchsms.default_sender')
);
$apiMessage = (new OutboundMessage())
->setTo($to)
->setFrom($message->sender ?? config('services.touchsms.default_sender'))
->setBody($message->content)
->setReference($message->reference)
->setMetadata($message->metadata);

if ($response->code !== 200) {
throw CouldNotSendNotification::touchSmsError($response->message ?? '', $response->code ?? 500);
if ($message->sendAt) {
$apiMessage->setDate($message->sendAt->format(\DateTimeInterface::ATOM));
}

$response = $this->client->sendMessages(new SendMessageBody([
'messages' => [$apiMessage],
]));

if (! $response || count($response->getData()->getErrors())) {
$error = $response->getData()->getErrors()[0];
throw CouldNotSendNotification::touchSmsError($error->getErrorCode().$error->getErrorHelp() ? ' - '.$error->getErrorHelp() : '', 400);
}
}
}
20 changes: 15 additions & 5 deletions src/TouchSmsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
class TouchSmsMessage
{
/** @var string */
public $content;
public string $content;

/** @var string|null */
public $sender;
public ?string $sender;

/** @var string|null */
public $campaign;
public ?string $campaign;

/** @var string|null */
public $reference;
public ?string $reference;

/** @var DateTimeInterface|null */
public $sendAt;
public ?DateTimeInterface $sendAt;

/** @var array|null */
public ?array $metadata;

public function __construct(string $content = '')
{
Expand Down Expand Up @@ -54,6 +57,13 @@ public function campaign(string $campaign): self
return $this;
}

public function metadata(array $metadata): self
{
$this->metadata = $metadata;

return $this;
}

public function reference(string $reference): self
{
$this->reference = $reference;
Expand Down
33 changes: 15 additions & 18 deletions src/TouchSmsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,28 @@

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use TouchSMS\TouchSMS\touchSMS;
use TouchSms\ApiClient\Client;
use TouchSms\ApiClient\ClientFactory;

class TouchSmsServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register the application services.
*/
public function register()
public function register(): void
{
$this->app->singleton(touchSMS::class, function ($app) {
if (empty($app['config']['services.touchsms.token_id'])
|| empty($app['config']['services.touchsms.access_token'])) {
throw new \InvalidArgumentException('Missing TouchSMS config in services');
}
$this->app->when(TouchSmsChannel::class)
->needs(Client::class)
->give(function ($app) {
if (empty($app['config']['services.touchsms.token_id'])
|| empty($app['config']['services.touchsms.access_token'])) {
throw new \InvalidArgumentException('Missing TouchSMS config in services');
}

return new touchSMS(
$app['config']['services.touchsms.access_token'],
$app['config']['services.touchsms.token_id'],
$app['config']['services.touchsms.sandbox'] ?? false
);
});
}

public function provides()
{
return [touchSMS::class];
return ClientFactory::create(
$app['config']['services.touchsms.access_token'],
$app['config']['services.touchsms.token_id']
);
});
}
}

0 comments on commit f39e903

Please sign in to comment.