Skip to content

Commit

Permalink
Merge pull request #9 from laravel-notification-channels/dev
Browse files Browse the repository at this point in the history
Make polling interval configurable; Better handling of errors from API
  • Loading branch information
iv-craig committed May 11, 2021
2 parents c0ffdb9 + d906e4e commit 42e0bac
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -44,12 +44,15 @@ This channel will use your InterFAX username and password. To use the channel, a
'username' => env('INTERFAX_USERNAME'),
'password' => env('INTERFAX_PASSWORD'),
'pci' => env('INTERFAX_PCI', false),
'interval' => 15,
],
...
```

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.

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.

## Usage

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.
Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/CouldNotSendNotification.php
Expand Up @@ -18,9 +18,9 @@ public function __construct($message, $code, Exception $previous = null, Interfa
$this->responseAttributes = $responseAttributes;
}

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

public function getUser()
Expand Down
31 changes: 19 additions & 12 deletions src/InterfaxChannel.php
Expand Up @@ -31,19 +31,26 @@ public function send($notifiable, Notification $notification)

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

$fax = $this->client->deliver([
'faxNumber' => $faxNumber,
'files' => $message->makeFiles(),
]);

if ($message->shouldCheckStatus()) {
while ($fax->refresh()->status < 0) {
sleep(config('services.interfax.interval', 15));
}

if ($fax->refresh()->status > 0) {
throw CouldNotSendNotification::serviceRespondedWithAnError($message, $fax->refresh()->attributes());
try {
$fax = $this->client->deliver([
'faxNumber' => $faxNumber,
'files' => $message->makeFiles(),
]);

if ($message->shouldCheckStatus()) {
$message->sleep();

while ($fax->refresh()->status < 0) {
$message->sleep();
}

if ($fax->refresh()->status > 0) {
throw CouldNotSendNotification::serviceRespondedWithAnError($message, $fax->refresh()->attributes());
}
}
} catch (\Interfax\Exception\RequestException $e) {
$exceptionMessage = $e->getMessage().': '.($e->getWrappedException())->getMessage();
throw CouldNotSendNotification::serviceRespondedWithAnError($message, $fax->refresh()->attributes(), $exceptionMessage);
}
}
}
9 changes: 9 additions & 0 deletions src/InterfaxMessage.php
Expand Up @@ -16,6 +16,9 @@ class InterfaxMessage
const FILES = 'files';
const STREAM = 'stream';

const POLLING_INTERVAL_DEFAULT = 15;
const POLLING_INTERVAL_MINIMUM = 10;

public function file(string $file)
{
$this->files = Arr::wrap($file);
Expand Down Expand Up @@ -81,4 +84,10 @@ public function makeFiles(): array

return $this->files;
}

public function sleep(): void
{
$interval = config('services.interfax.interval', static::POLLING_INTERVAL_DEFAULT);
sleep(max($interval, static::POLLING_INTERVAL_MINIMUM));
}
}
24 changes: 24 additions & 0 deletions tests/CouldNotSendNotificationExceptionTest.php
Expand Up @@ -29,6 +29,30 @@ public function it_can_get_the_exception_user()
$this->assertInstanceOf(TestNotifiable::class, $exception->getUser());
}

/** @test */
public function it_can_get_the_default_exception_message()
{
$exception = CouldNotSendNotification::serviceRespondedWithAnError($this->message, [
'status' => 500,
'message' => 'Failed to send.',
]);

$this->assertSame('The fax failed to send via InterFAX.', $exception->getMessage());
}

/** @test */
public function it_can_get_a_custom_exception_message()
{
$exceptionMessage = 'This is a test.';

$exception = CouldNotSendNotification::serviceRespondedWithAnError($this->message, [
'status' => 500,
'message' => 'Failed to send.',
], $exceptionMessage);

$this->assertSame($exceptionMessage, $exception->getMessage());
}

/** @test */
public function it_can_get_the_exception_attributes()
{
Expand Down

0 comments on commit 42e0bac

Please sign in to comment.