Skip to content

Commit f39e903

Browse files
authored
feat: use v2 api client, add metadata (#3)
1 parent 072afbb commit f39e903

File tree

5 files changed

+64
-42
lines changed

5 files changed

+64
-42
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ jobs:
1010
strategy:
1111
fail-fast: true
1212
matrix:
13-
php: [7.3, 7.4, 8.0]
13+
php: [8.2]
1414

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

1717
steps:
1818
- name: Checkout code
19-
uses: actions/checkout@v2
19+
uses: actions/checkout@v3
2020

2121
- name: Setup PHP
2222
uses: shivammathur/setup-php@v2

composer.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=7.2",
16-
"illuminate/notifications": "~5.0 || ~6.0 || ~7.0 || ~8.0 || ~9.0 || ~10.0",
17-
"illuminate/support": "~5.0 || ~6.0 || ~7.0 || ~8.0 || ~9.0 || ~10.0",
18-
"touchsms/touchsms": "^1.3"
15+
"php": ">=8.2",
16+
"illuminate/notifications": "~8.0 || ~9.0 || ~10.0 || ~11.0",
17+
"illuminate/support": "~8.0 || ~9.0 || ~10.0 || ~11.0",
18+
"symfony/http-client": "^7.0",
19+
"touchsms/touchsms": "^2.0"
1920
},
2021
"require-dev": {
2122
"mockery/mockery": "^1.0",
@@ -43,6 +44,9 @@
4344
}
4445
},
4546
"config": {
46-
"sort-packages": true
47+
"sort-packages": true,
48+
"allow-plugins": {
49+
"php-http/discovery": true
50+
}
4751
}
4852
}

src/TouchSmsChannel.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
use Illuminate\Notifications\Notification;
66
use NotificationChannels\TouchSms\Exceptions\CouldNotSendNotification;
7-
use TouchSMS\TouchSMS\touchSMS;
7+
use TouchSms\ApiClient\Api\Model\OutboundMessage;
8+
use TouchSms\ApiClient\Api\Model\SendMessageBody;
9+
use TouchSms\ApiClient\Client;
810

911
class TouchSmsChannel
1012
{
11-
/** @var touchSMS */
12-
private $client;
13+
/** @var Client */
14+
private Client $client;
1315

14-
public function __construct(touchSMS $client)
16+
public function __construct(Client $client)
1517
{
1618
$this->client = $client;
1719
}
@@ -38,15 +40,24 @@ public function send($notifiable, Notification $notification): void
3840
return;
3941
}
4042

41-
$response = $this->client->sendMessage(
42-
$message->content,
43-
$to,
44-
$message->reference,
45-
$message->sender ?? config('services.touchsms.default_sender')
46-
);
43+
$apiMessage = (new OutboundMessage())
44+
->setTo($to)
45+
->setFrom($message->sender ?? config('services.touchsms.default_sender'))
46+
->setBody($message->content)
47+
->setReference($message->reference)
48+
->setMetadata($message->metadata);
4749

48-
if ($response->code !== 200) {
49-
throw CouldNotSendNotification::touchSmsError($response->message ?? '', $response->code ?? 500);
50+
if ($message->sendAt) {
51+
$apiMessage->setDate($message->sendAt->format(\DateTimeInterface::ATOM));
52+
}
53+
54+
$response = $this->client->sendMessages(new SendMessageBody([
55+
'messages' => [$apiMessage],
56+
]));
57+
58+
if (! $response || count($response->getData()->getErrors())) {
59+
$error = $response->getData()->getErrors()[0];
60+
throw CouldNotSendNotification::touchSmsError($error->getErrorCode().$error->getErrorHelp() ? ' - '.$error->getErrorHelp() : '', 400);
5061
}
5162
}
5263
}

src/TouchSmsMessage.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@
77
class TouchSmsMessage
88
{
99
/** @var string */
10-
public $content;
10+
public string $content;
1111

1212
/** @var string|null */
13-
public $sender;
13+
public ?string $sender;
1414

1515
/** @var string|null */
16-
public $campaign;
16+
public ?string $campaign;
1717

1818
/** @var string|null */
19-
public $reference;
19+
public ?string $reference;
2020

2121
/** @var DateTimeInterface|null */
22-
public $sendAt;
22+
public ?DateTimeInterface $sendAt;
23+
24+
/** @var array|null */
25+
public ?array $metadata;
2326

2427
public function __construct(string $content = '')
2528
{
@@ -54,6 +57,13 @@ public function campaign(string $campaign): self
5457
return $this;
5558
}
5659

60+
public function metadata(array $metadata): self
61+
{
62+
$this->metadata = $metadata;
63+
64+
return $this;
65+
}
66+
5767
public function reference(string $reference): self
5868
{
5969
$this->reference = $reference;

src/TouchSmsServiceProvider.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,28 @@
44

55
use Illuminate\Contracts\Support\DeferrableProvider;
66
use Illuminate\Support\ServiceProvider;
7-
use TouchSMS\TouchSMS\touchSMS;
7+
use TouchSms\ApiClient\Client;
8+
use TouchSms\ApiClient\ClientFactory;
89

910
class TouchSmsServiceProvider extends ServiceProvider implements DeferrableProvider
1011
{
1112
/**
1213
* Register the application services.
1314
*/
14-
public function register()
15+
public function register(): void
1516
{
16-
$this->app->singleton(touchSMS::class, function ($app) {
17-
if (empty($app['config']['services.touchsms.token_id'])
18-
|| empty($app['config']['services.touchsms.access_token'])) {
19-
throw new \InvalidArgumentException('Missing TouchSMS config in services');
20-
}
17+
$this->app->when(TouchSmsChannel::class)
18+
->needs(Client::class)
19+
->give(function ($app) {
20+
if (empty($app['config']['services.touchsms.token_id'])
21+
|| empty($app['config']['services.touchsms.access_token'])) {
22+
throw new \InvalidArgumentException('Missing TouchSMS config in services');
23+
}
2124

22-
return new touchSMS(
23-
$app['config']['services.touchsms.access_token'],
24-
$app['config']['services.touchsms.token_id'],
25-
$app['config']['services.touchsms.sandbox'] ?? false
26-
);
27-
});
28-
}
29-
30-
public function provides()
31-
{
32-
return [touchSMS::class];
25+
return ClientFactory::create(
26+
$app['config']['services.touchsms.access_token'],
27+
$app['config']['services.touchsms.token_id']
28+
);
29+
});
3330
}
3431
}

0 commit comments

Comments
 (0)