Skip to content

Commit 42e0bac

Browse files
authored
Merge pull request #9 from laravel-notification-channels/dev
Make polling interval configurable; Better handling of errors from API
2 parents c0ffdb9 + d906e4e commit 42e0bac

File tree

5 files changed

+57
-14
lines changed

5 files changed

+57
-14
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ This channel will use your InterFAX username and password. To use the channel, a
4444
'username' => env('INTERFAX_USERNAME'),
4545
'password' => env('INTERFAX_PASSWORD'),
4646
'pci' => env('INTERFAX_PCI', false),
47+
'interval' => 15,
4748
],
4849
...
4950
```
5051

5152
This will load your InterFAX credentials from the `.env` file. If your requests must be PCI-DSS-compliant, set `INTERFAX_PCI=true` in your `.env` file.
5253

54+
The `services.interfax.interval` setting is the polling interval, in seconds, for a fax if it is set to check the status until it is complete. This is optional and will default to 15 if left empty. The interval has a minimum of 10 seconds, as the outbound service in the API has a maximum freqncy of 6 requests per minute and can return errors if polled more frequently.
55+
5356
## Usage
5457

5558
To use this package, you can create a notification class, like `DocumentWasSent` from the example below, in your Laravel application. Make sure to check out [Laravel's documentation](https://laravel.com/docs/master/notifications) for this process.

src/Exceptions/CouldNotSendNotification.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public function __construct($message, $code, Exception $previous = null, Interfa
1818
$this->responseAttributes = $responseAttributes;
1919
}
2020

21-
public static function serviceRespondedWithAnError($message, $responseAttributes)
21+
public static function serviceRespondedWithAnError($message, $responseAttributes, string $exceptionMessage = 'The fax failed to send via InterFAX.')
2222
{
23-
return new static('The fax failed to send via InterFAX.', $responseAttributes['status'], null, $message, $responseAttributes);
23+
return new static($exceptionMessage, $responseAttributes['status'], null, $message, $responseAttributes);
2424
}
2525

2626
public function getUser()

src/InterfaxChannel.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,26 @@ public function send($notifiable, Notification $notification)
3131

3232
$message = $notification->toInterfax($notifiable);
3333

34-
$fax = $this->client->deliver([
35-
'faxNumber' => $faxNumber,
36-
'files' => $message->makeFiles(),
37-
]);
38-
39-
if ($message->shouldCheckStatus()) {
40-
while ($fax->refresh()->status < 0) {
41-
sleep(config('services.interfax.interval', 15));
42-
}
43-
44-
if ($fax->refresh()->status > 0) {
45-
throw CouldNotSendNotification::serviceRespondedWithAnError($message, $fax->refresh()->attributes());
34+
try {
35+
$fax = $this->client->deliver([
36+
'faxNumber' => $faxNumber,
37+
'files' => $message->makeFiles(),
38+
]);
39+
40+
if ($message->shouldCheckStatus()) {
41+
$message->sleep();
42+
43+
while ($fax->refresh()->status < 0) {
44+
$message->sleep();
45+
}
46+
47+
if ($fax->refresh()->status > 0) {
48+
throw CouldNotSendNotification::serviceRespondedWithAnError($message, $fax->refresh()->attributes());
49+
}
4650
}
51+
} catch (\Interfax\Exception\RequestException $e) {
52+
$exceptionMessage = $e->getMessage().': '.($e->getWrappedException())->getMessage();
53+
throw CouldNotSendNotification::serviceRespondedWithAnError($message, $fax->refresh()->attributes(), $exceptionMessage);
4754
}
4855
}
4956
}

src/InterfaxMessage.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class InterfaxMessage
1616
const FILES = 'files';
1717
const STREAM = 'stream';
1818

19+
const POLLING_INTERVAL_DEFAULT = 15;
20+
const POLLING_INTERVAL_MINIMUM = 10;
21+
1922
public function file(string $file)
2023
{
2124
$this->files = Arr::wrap($file);
@@ -81,4 +84,10 @@ public function makeFiles(): array
8184

8285
return $this->files;
8386
}
87+
88+
public function sleep(): void
89+
{
90+
$interval = config('services.interfax.interval', static::POLLING_INTERVAL_DEFAULT);
91+
sleep(max($interval, static::POLLING_INTERVAL_MINIMUM));
92+
}
8493
}

tests/CouldNotSendNotificationExceptionTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ public function it_can_get_the_exception_user()
2929
$this->assertInstanceOf(TestNotifiable::class, $exception->getUser());
3030
}
3131

32+
/** @test */
33+
public function it_can_get_the_default_exception_message()
34+
{
35+
$exception = CouldNotSendNotification::serviceRespondedWithAnError($this->message, [
36+
'status' => 500,
37+
'message' => 'Failed to send.',
38+
]);
39+
40+
$this->assertSame('The fax failed to send via InterFAX.', $exception->getMessage());
41+
}
42+
43+
/** @test */
44+
public function it_can_get_a_custom_exception_message()
45+
{
46+
$exceptionMessage = 'This is a test.';
47+
48+
$exception = CouldNotSendNotification::serviceRespondedWithAnError($this->message, [
49+
'status' => 500,
50+
'message' => 'Failed to send.',
51+
], $exceptionMessage);
52+
53+
$this->assertSame($exceptionMessage, $exception->getMessage());
54+
}
55+
3256
/** @test */
3357
public function it_can_get_the_exception_attributes()
3458
{

0 commit comments

Comments
 (0)