Skip to content

Commit 8f15d72

Browse files
committed
feat: custom client configuration usage via per notification or notifiable
Update Textlocal.php, TextlocalChannel.php, TextlocalServiceProvider.php Create INotificationUsesTextlocalClientConfig.php Create IUsesTextlocalClientConfig.php Update README.md
1 parent bc69e64 commit 8f15d72

File tree

6 files changed

+64
-20
lines changed

6 files changed

+64
-20
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@ return [
4848
'UK' => 'https://api.txtlocal.com/'
4949
],
5050
'country' => env('TEXTLOCAL_COUNTRY', 'IN'),
51+
'extra' => [
52+
// extra config define keys as desired for use within the textlocal client config
53+
// if you want to use the custom client config for each notification or notifiable
54+
// INotificationUsesTextlocalClientConfig
55+
]
5156
];
5257
```
53-
### Configuring .env
58+
### Configuring .env
5459
```
5560
TEXTLOCAL_USERNAME=Your email id or api key
5661
TEXTLOCAL_HASH=get it from url '/docs/' under your API KEYS section
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace NotificationChannels\Textlocal\Contracts;
4+
5+
interface INotificationUsesTextlocalClientConfig
6+
{
7+
/**
8+
* @param $notifiable
9+
* @return array [$username, $hash, $apiKey, $country]
10+
*/
11+
public function getTextlocalClientConfig($notifiable): array;
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace NotificationChannels\Textlocal\Contracts;
4+
5+
interface IUsesTextlocalClientConfig
6+
{
7+
/**
8+
* @param $notification
9+
* @return array [$username, $hash, $apiKey, $country]
10+
*/
11+
public function getTextlocalClientConfig($notification): array;
12+
}

src/Textlocal.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ class Textlocal
2222
const REQUEST_TIMEOUT = 60;
2323
const REQUEST_HANDLER = 'curl';
2424

25-
private $request_url;
26-
private $country;
27-
28-
private $username;
29-
private $hash;
30-
private $apiKey;
25+
private string $request_url;
3126

3227
private $errorReporting = false;
3328

@@ -40,20 +35,14 @@ class Textlocal
4035
/**
4136
* Instantiate the object.
4237
*
43-
* @param $username
44-
* @param $hash
38+
* @param string|null $username
39+
* @param string|null $hash
4540
* @param string|null $apiKey
41+
* @param string $country
4642
*/
47-
public function __construct($username, $hash, $apiKey = null)
43+
public function __construct(private $username, private $hash, private $apiKey, private string $country)
4844
{
49-
$this->username = $username;
50-
$this->hash = $hash;
51-
if (! is_null($apiKey)) {
52-
$this->apiKey = $apiKey;
53-
}
54-
55-
$this->country = config('textlocal.country');
56-
$this->request_url = config('textlocal.request_urls')[$this->country];
45+
$this->request_url = config('textlocal.request_urls')[$country];
5746
}
5847

5948
/**

src/TextlocalChannel.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace NotificationChannels\Textlocal;
44

55
use Illuminate\Notifications\Notification;
6+
use NotificationChannels\Textlocal\Contracts\INotificationUsesTextlocalClientConfig;
7+
use NotificationChannels\Textlocal\Contracts\IUsesTextlocalClientConfig;
68
use NotificationChannels\Textlocal\Exceptions\CouldNotSendNotification;
79

810
/**
@@ -66,8 +68,10 @@ public function send($notifiable, Notification $notification)
6668
$this->sender = $notification->getSenderId();
6769
}
6870

71+
$client = $this->getClient($notifiable, $notification);
72+
6973
try {
70-
$response = $this->client
74+
$response = $client
7175
->setUnicodeMode($unicode)
7276
->sendSms($numbers, $message, $this->sender);
7377

@@ -76,4 +80,25 @@ public function send($notifiable, Notification $notification)
7680
throw CouldNotSendNotification::serviceRespondedWithAnError($exception, $message);
7781
}
7882
}
83+
84+
public function getClient($notifiable, Notification $notification)
85+
{
86+
$client = $this->client;
87+
88+
if ($notifiable instanceof IUsesTextlocalClientConfig) {
89+
90+
[$username, $hash, $apiKey, $country] = $notification->getTextlocalClientConfig($notification);
91+
92+
$client = new Textlocal($username, $hash, $apiKey, $country);
93+
}
94+
95+
if ($notification instanceof INotificationUsesTextlocalClientConfig) {
96+
97+
[$username, $hash, $apiKey, $country] = $notification->getTextlocalClientConfig($notifiable);
98+
99+
$client = new Textlocal($username, $hash, $apiKey, $country);
100+
}
101+
102+
return $client;
103+
}
79104
}

src/TextlocalServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function () {
2222
return new Textlocal(
2323
$config['username'],
2424
$config['hash'],
25-
$config['api_key']
25+
$config['api_key'],
26+
$config['country']
2627
);
2728
}
2829
);

0 commit comments

Comments
 (0)