1
1
# Laravel SmsApi Notification Channel
2
2
3
- [ ![ Latest Version on Packagist ] ( https://img.shields.io/packagist/v/webxscripts/laravel-smsapi-notification-channel.svg?style=flat-square )] ( https://packagist.org/packages/webxscripts/laravel-smsapi-notification-channel )
4
- [ ![ GitHub Tests Action Status ] ( https://img.shields.io/github/actions/workflow/status/webxscripts/laravel-smsapi-notification-channel/run-tests.yml?branch=main&label=tests&style=flat-square )] ( https://github.com/webxscripts/laravel-smsapi-notification-channel/actions?query=workflow%3Arun-tests+branch%3Amain )
5
- [ ![ GitHub Code Style Action Status ] ( https://img.shields.io/github/actions/workflow/status/webxscripts/laravel-smsapi-notification-channel/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square )] ( https://github.com/webxscripts/laravel-smsapi-notification-channel/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain )
3
+ [ ![ Latest Version] ( https://img.shields.io/packagist/v/webxscripts/laravel-smsapi-notification-channel.svg?style=flat-square )] ( https://packagist.org/packages/webxscripts/laravel-smsapi-notification-channel )
4
+ [ ![ Tests] ( https://img.shields.io/github/actions/workflow/status/webxscripts/laravel-smsapi-notification-channel/run-tests.yml?branch=main&label=tests&style=flat-square )] ( https://github.com/webxscripts/laravel-smsapi-notification-channel/actions?query=workflow%3Arun-tests+branch%3Amain )
5
+ [ ![ Code Style] ( https://img.shields.io/github/actions/workflow/status/webxscripts/laravel-smsapi-notification-channel/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square )] ( https://github.com/webxscripts/laravel-smsapi-notification-channel/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain )
6
6
[ ![ Total Downloads] ( https://img.shields.io/packagist/dt/webxscripts/laravel-smsapi-notification-channel.svg?style=flat-square )] ( https://packagist.org/packages/webxscripts/laravel-smsapi-notification-channel )
7
7
8
- Modern Laravel Notification Channel for SmsApi PHP Client with support for Laravel 11+ and PHP 8.2+.
8
+ A Laravel 11+ notification channel for [ SmsApi] ( https://www.smsapi.com/ ) with PHP 8.2+ support .
9
9
10
- ## Features
10
+ ---
11
11
12
- - 🚀 ** Modern PHP 8.2+ features** - Readonly classes, named arguments, match expressions
13
- - 📱 ** Full SmsApi support** - All SmsApi features including templates, parameters, and delivery options
14
- - 🔧 ** Type-safe** - Full type hints and PHPStan level 8 compliance
15
- - 🧪 ** Well tested** - Comprehensive test suite with Pest PHP
16
- - 🎯 ** Laravel 11+ optimized** - Built specifically for modern Laravel versions
17
- - 🔒 ** Immutable messages** - Thread-safe message building with fluent interface
18
- - 📋 ** Multiple phone resolution methods** - Flexible ways to get phone numbers from notifiables
12
+ ## Requirements
19
13
20
- ## Installation
14
+ - PHP 8.2 or higher
15
+ - Laravel 11 or newer
16
+ - SmsApi account and API token
21
17
22
- ``` bash
23
- composer require webxscripts/laravel-smsapi-notification-channel
24
- ```
18
+ ---
25
19
26
- Publish the configuration file:
20
+ ## Installation
27
21
28
22
``` bash
23
+ composer require webxscripts/laravel-smsapi-notification-channel
29
24
php artisan vendor:publish --tag=smsapi-config
30
25
```
31
26
27
+ ---
28
+
32
29
## Configuration
33
30
34
- Add these environment variables to your ` .env ` file:
31
+ Add the following to your ` .env ` file:
35
32
36
33
``` env
37
34
SMSAPI_TOKEN=your_api_token_here
38
35
SMSAPI_SERVICE=com
39
36
SMSAPI_FROM=YourApp
40
37
```
41
38
42
- ### Available Services
43
-
44
- - ` com ` - SMSAPI.COM (international, default)
45
- - ` pl ` - SMSAPI.PL (Poland)
46
-
47
- For SMSAPI.SE or SMSAPI.BG, use:
48
- ``` env
49
- SMSAPI_SERVICE=com
50
- SMSAPI_URI=https://api.smsapi.se/
51
- ```
39
+ Supported services:
40
+ - ` com ` (default)
41
+ - ` pl `
42
+ - For other regions, use:
43
+ ``` env
44
+ SMSAPI_URI=https://api.smsapi.se/
45
+ ```
52
46
53
- ## Usage
47
+ ---
54
48
55
- ### Basic Usage
49
+ ## Basic Usage
56
50
57
51
Create a notification:
58
52
59
53
``` bash
60
54
php artisan make:notification OrderConfirmation
61
55
```
62
56
63
- Implement the ` toSmsApi ` method :
57
+ Example implementation :
64
58
65
59
``` php
66
- <?php
67
-
68
- namespace App\Notifications;
69
-
70
60
use Illuminate\Notifications\Notification;
71
61
use WebXScripts\SmsApiNotification\SmsApiMessage;
72
62
73
63
class OrderConfirmation extends Notification
74
64
{
75
- public function __construct(
76
- private readonly string $orderNumber
77
- ) {}
65
+ public function __construct(private string $orderNumber) {}
78
66
79
67
public function via($notifiable): array
80
68
{
@@ -83,150 +71,121 @@ class OrderConfirmation extends Notification
83
71
84
72
public function toSmsApi($notifiable): SmsApiMessage
85
73
{
86
- return SmsApiMessage::create("Your order #{$this->orderNumber} has been confirmed! ")
74
+ return SmsApiMessage::create("Order #{$this->orderNumber} confirmed. ")
87
75
->from('MyShop');
88
76
}
89
77
}
90
78
```
91
79
92
- ### Advanced Usage with All Options
80
+ ---
81
+
82
+ ## Advanced Example
93
83
94
84
``` php
95
- public function toSmsApi($notifiable): SmsApiMessage
96
- {
97
- return SmsApiMessage::create('Your verification code: 123456')
98
- ->from('MyApp')
99
- ->encoding('utf-8')
100
- ->test(app()->environment('testing'))
101
- ->fast(true)
102
- ->normalize(true)
103
- ->single(false)
104
- ->expirationDate(now()->addMinutes(5))
105
- ->notifyUrl('https://example.com/sms-delivery-report')
106
- ->template('verification_code')
107
- ->param1($this->code)
108
- ->param2($notifiable->name);
109
- }
85
+ SmsApiMessage::create('Code: 123456')
86
+ ->from('MyApp')
87
+ ->encoding('utf-8')
88
+ ->test(app()->environment('testing'))
89
+ ->fast(true)
90
+ ->normalize(true)
91
+ ->single(false)
92
+ ->expirationDate(now()->addMinutes(5))
93
+ ->notifyUrl('https://example.com/sms-delivery')
94
+ ->template('verification_code')
95
+ ->param1($this->code)
96
+ ->param2($notifiable->name);
110
97
```
111
98
112
- ### Phone Number Resolution
113
-
114
- The channel resolves phone numbers in this priority order:
99
+ ---
115
100
116
- 1 . ** SmsApiNotifiable interface** (recommended):
117
- ``` php
118
- use WebXScripts\SmsApiNotification\Contracts\SmsApiNotifiable;
119
-
120
- class User extends Model implements SmsApiNotifiable
121
- {
122
- public function getSmsApiPhoneNumber(): string
123
- {
124
- return $this->phone_number;
125
- }
126
- }
127
- ```
101
+ ## Phone Number Resolution Order
128
102
129
- 2 . ** Notification routing method** :
130
- ``` php
131
- class User extends Model
132
- {
133
- public function routeNotificationForSmsApi(): string
134
- {
135
- return $this->phone_number;
136
- }
137
- }
138
- ```
103
+ 1 . ` SmsApiNotifiable ` interface:
104
+ ``` php
105
+ public function getSmsApiPhoneNumber(): string
106
+ {
107
+ return $this->phone_number;
108
+ }
109
+ ```
139
110
140
- 3 . ** Generic SMS routing method** :
141
- ``` php
142
- public function routeNotificationForSms(): string
143
- {
144
- return $this->phone;
145
- }
146
- ```
111
+ 2 . ` routeNotificationForSmsApi() ` method
112
+ 3 . ` routeNotificationForSms() ` method
113
+ 4 . Model attributes: ` phone ` or ` phone_number `
147
114
148
- 4 . ** Model attributes ** : ` phone ` or ` phone_number `
115
+ ---
149
116
150
- ### Sending Notifications
117
+ ## Sending Notifications
151
118
152
119
``` php
153
- use App\Notifications\OrderConfirmation;
154
-
155
- // Single user
156
- $user = User::find(1);
157
120
$user->notify(new OrderConfirmation('ORD-12345'));
158
121
159
- // Multiple users
160
- $users = User::whereNotNull('phone')->get();
161
122
Notification::send($users, new OrderConfirmation('ORD-12345'));
162
123
163
- // On-demand notifications
164
124
Notification::route('smsapi', '+48123456789')
165
125
->notify(new OrderConfirmation('ORD-12345'));
166
126
```
167
127
168
- ## Available Message Methods
128
+ ---
169
129
170
- All methods return a new immutable instance:
130
+ ## Available Message Methods
171
131
172
132
``` php
173
- SmsApiMessage::create('content ')
174
- ->from(string $sender) // Sender name/number
175
- ->encoding(string $encoding) // Message encoding (default: utf-8 )
176
- ->test(bool $test) // Test mode
177
- ->fast(bool $fast) // Fast delivery
178
- ->normalize(bool $normalize) // Normalize phone numbers
179
- ->noUnicode(bool $noUnicode) // Disable unicode
180
- ->single(bool $single) // Send as single message
181
- ->notifyUrl(string $url) // Delivery report webhook URL
182
- ->expirationDate(DateTimeInterface $date) // Message expiration
183
- ->timeRestriction(string $restriction) // Time-based delivery restrictions
184
- ->partnerId(string $partnerId) // Partner ID
185
- ->checkIdx(bool $checkIdx) // Validate IDX
186
- ->idx(array $idx) // IDX array for external tracking
187
- ->template(string $template) // Template name
188
- ->param1(string $param) // Template parameter 1
189
- ->param2(string $param) // Template parameter 2
190
- ->param3(string $param) // Template parameter 3
191
- ->param4(string $param); // Template parameter 4
133
+ SmsApiMessage::create('... ')
134
+ ->from(string)
135
+ ->encoding(string)
136
+ ->test(bool)
137
+ ->fast(bool)
138
+ ->normalize(bool)
139
+ ->noUnicode(bool)
140
+ ->single(bool)
141
+ ->notifyUrl(string)
142
+ ->expirationDate(DateTimeInterface)
143
+ ->timeRestriction(string)
144
+ ->partnerId(string)
145
+ ->checkIdx(bool)
146
+ ->idx(array)
147
+ ->template(string)
148
+ ->param1(string)
149
+ ->param2(string)
150
+ ->param3(string)
151
+ ->param4(string);
192
152
```
193
153
154
+ All methods return an immutable instance.
155
+
156
+ ---
157
+
194
158
## Error Handling
195
159
196
- The package includes specific exceptions:
160
+ Custom exceptions provided :
197
161
198
- ``` php
199
- use WebXScripts\SmsApiNotification\Exceptions\{
200
- InvalidNotificationException,
201
- MissingApiTokenException,
202
- MissingPhoneNumberException
203
- };
162
+ - ` MissingPhoneNumberException `
163
+ - ` MissingApiTokenException `
164
+ - ` InvalidNotificationException `
165
+
166
+ Example:
204
167
168
+ ``` php
205
169
try {
206
170
$user->notify(new OrderConfirmation('ORD-12345'));
207
171
} catch (MissingPhoneNumberException $e) {
208
- Log::warning('User has no phone number', ['user' => $user->id]);
209
- } catch (MissingApiTokenException $e) {
210
- Log::error('SmsApi token not configured');
172
+ Log::warning('User has no phone number');
211
173
}
212
174
```
213
175
214
- ## Testing
215
-
216
- ``` bash
217
- # Run tests
218
- composer test
219
-
220
- # Run tests with coverage
221
- composer test-coverage
176
+ ---
222
177
223
- # Format code
224
- composer format
178
+ ## Testing & Code Quality
225
179
226
- # Analyze code
227
- composer analyse
180
+ ``` bash
181
+ composer test # Run tests
182
+ composer test-coverage # Run with coverage
183
+ composer format # Format code
184
+ composer analyse # Static analysis
228
185
```
229
186
187
+ ---
188
+
230
189
## License
231
190
232
- The MIT License (MIT). Please see [ License File ] ( LICENSE.md ) for more information .
191
+ MIT. See [ LICENSE.md ] ( LICENSE.md ) .
0 commit comments