Skip to content

Commit

Permalink
Merge pull request #17 from ivinteractive/main
Browse files Browse the repository at this point in the history
Bump PHPStan to level 8; Add more parameter/return typing; README update
  • Loading branch information
iv-craig authored Oct 8, 2024
2 parents 3e793a3 + fcf6888 commit 9ec4ad5
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 30 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# InterFAX notification channel for Laravel 9.x, 10.x
# InterFAX notification channel for Laravel 10.x, 11.x

[![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/interfax.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/interfax)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/laravel-notification-channels/interfax/main.svg?style=flat-square)](https://travis-ci.org/laravel-notification-channels/interfax)
[![Total Downloads](https://img.shields.io/packagist/dt/laravel-notification-channels/interfax.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/interfax)

This package makes it easy to send notifications using [InterFAX](https://interfax.net) with Laravel 9.x and 10.x.
This package makes it easy to send notifications using [InterFAX](https://interfax.net) with Laravel 10.x and 11.x.

## Contents

Expand Down Expand Up @@ -140,4 +140,4 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
10 changes: 9 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ parameters:
paths:
- src/

level: 5
level: 8

ignoreErrors:
-
identifier: ternary.alwaysTrue
path: src/InterfaxChannel.php

stubFiles:
- stubs/Resource.stub
4 changes: 4 additions & 0 deletions src/Contracts/InterfaxNotificationContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@

interface InterfaxNotificationContract
{
/**
* @param mixed $notifiable
* @return InterfaxMessage
*/
public function toInterfax($notifiable): InterfaxMessage;
}
34 changes: 29 additions & 5 deletions src/Exceptions/CouldNotSendNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@

class CouldNotSendNotification extends Exception
{
protected $interfaxMessage;
protected $responseAttributes;
protected InterfaxMessage $interfaxMessage;

/** @var array<string, mixed> */
protected array $responseAttributes;

/**
* @param string $message
* @param int $code
* @param Exception|null $previous
* @param InterfaxMessage $interfaxMessage
* @param array<string, mixed> $responseAttributes
*/
final public function __construct(string $message, int $code, Exception $previous = null, InterfaxMessage $interfaxMessage, array $responseAttributes)
{
parent::__construct($message, $code, $previous);
Expand All @@ -18,22 +27,37 @@ final public function __construct(string $message, int $code, Exception $previou
$this->responseAttributes = $responseAttributes;
}

public static function serviceRespondedWithAnError($message, $responseAttributes, string $exceptionMessage = 'The fax failed to send via InterFAX.')
/**
* @param InterfaxMessage $message
* @param array<string, mixed> $responseAttributes
* @param string $exceptionMessage
* @return CouldNotSendNotification
*/
public static function serviceRespondedWithAnError(InterfaxMessage $message, array $responseAttributes, string $exceptionMessage = 'The fax failed to send via InterFAX.')
{
return new static($exceptionMessage, $responseAttributes['status'], null, $message, $responseAttributes);
}

/**
* @return mixed
*/
public function getUser()
{
return $this->interfaxMessage->user;
}

public function getMetadata()
/**
* @return array<string, mixed>
*/
public function getMetadata(): array
{
return $this->interfaxMessage->metadata;
}

public function getAttributes()
/**
* @return array<string, mixed>
*/
public function getAttributes(): array
{
return $this->responseAttributes;
}
Expand Down
19 changes: 11 additions & 8 deletions src/InterfaxChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

class InterfaxChannel
{
protected $client;
/** @var \Interfax\Outbound\Fax $fax */
protected $fax;

public function __construct(Client $client)
{
$this->client = $client;
}
public function __construct(protected Client $client) {}

/**
* Send the given notification.
Expand All @@ -24,7 +21,7 @@ public function __construct(Client $client)
*
* @throws \NotificationChannels\Interfax\Exceptions\CouldNotSendNotification
*/
public function send($notifiable, InterfaxNotificationContract $notification)
public function send($notifiable, InterfaxNotificationContract $notification): void
{
if (! $faxNumber = $notifiable->routeNotificationFor('interfax')) {
return;
Expand All @@ -41,11 +38,11 @@ public function send($notifiable, InterfaxNotificationContract $notification)
if ($message->shouldCheckStatus()) {
$message->sleep();

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

if ($this->fax->refresh()->status > 0) {
if ($this->getStatus() > 0) {
throw CouldNotSendNotification::serviceRespondedWithAnError($message, $this->fax->attributes());
}
}
Expand All @@ -56,4 +53,10 @@ public function send($notifiable, InterfaxNotificationContract $notification)
throw CouldNotSendNotification::serviceRespondedWithAnError($message, $attributes, $exceptionMessage);
}
}

protected function getStatus(): int
{
$fax = $this->fax->refresh();
return $fax->status;
}
}
4 changes: 2 additions & 2 deletions src/InterfaxFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class InterfaxFile extends \Interfax\File
/**
* File constructor.
*
* @param $location
* @param array $params
* @param resource|string $location
* @param array<string, mixed> $params
*
* @throws \InvalidArgumentException
*/
Expand Down
39 changes: 29 additions & 10 deletions src/InterfaxMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

class InterfaxMessage
{
/** @var array<int, InterfaxFile|string|array<string, mixed>> */
protected array $files;

/** @var resource|false $stream */
protected $stream;
protected string $filename;
protected string $method;
protected $statusCheck = false;
public $user;
protected bool $statusCheck = false;
public mixed $user;

/** @var array<string, mixed> */
public array $metadata = [];

const FILES = 'files';
Expand All @@ -20,16 +25,20 @@ class InterfaxMessage
const POLLING_INTERVAL_DEFAULT = 15;
const POLLING_INTERVAL_MINIMUM = 10;

protected static $DEFAULT_CHUNK_SIZE = 1048576;
protected static int $DEFAULT_CHUNK_SIZE = 1048576;

public function file(string $file)
public function file(string $file): InterfaxMessage
{
$this->files = Arr::wrap($file);
$this->method = static::FILES;

return $this;
}

/**
* @param array<int, array<string, mixed>> $files
* @return InterfaxMessage
*/
public function files(array $files): InterfaxMessage
{
$this->files = $files;
Expand All @@ -38,6 +47,11 @@ public function files(array $files): InterfaxMessage
return $this;
}

/**
* @param resource|false $stream
* @param string $filename
* @return InterfaxMessage
*/
public function stream($stream, string $filename): InterfaxMessage
{
$this->stream = $stream;
Expand All @@ -47,10 +61,9 @@ public function stream($stream, string $filename): InterfaxMessage
return $this;
}

public function checkStatus(bool $shouldCheck = true)
public function checkStatus(bool $shouldCheck = true): InterfaxMessage
{
$this->statusCheck = $shouldCheck;

return $this;
}

Expand All @@ -68,14 +81,13 @@ public function shouldCheckStatus(): bool
public function user($notifiable): InterfaxMessage
{
$this->user = $notifiable;

return $this;
}

/**
* Add metadata to the message for logging purposes.
*
* @param array $data The data to add to the metadata array
* @param array<string, mixed> $data The data to add to the metadata array
* @return InterfaxMessage
*/
public function addMetadata(array $data): InterfaxMessage
Expand All @@ -87,6 +99,9 @@ public function addMetadata(array $data): InterfaxMessage
return $this;
}

/**
* @return array<int, InterfaxFile|array<string, mixed>>|array<int, array<int, mixed>>
*/
public function makeFiles(): array
{
if ($this->method === static::STREAM) {
Expand All @@ -102,7 +117,7 @@ public function makeFiles(): array
];
}

return array_map('static::setChunkSize', $this->files);
return array_map(fn ($file) => static::setChunkSize($file), $this->files);
}

public function sleep(): void
Expand All @@ -111,7 +126,11 @@ public function sleep(): void
sleep(max($interval, static::POLLING_INTERVAL_MINIMUM));
}

protected static function setChunkSize($file)
/**
* @param InterfaxFile|string|array<string, mixed> $file
* @return InterfaxFile|array<string, mixed>
*/
protected static function setChunkSize(InterfaxFile|string|array $file): InterfaxFile|array
{
$chunk_size = config('services.interfax.chunk_size', static::$DEFAULT_CHUNK_SIZE);

Expand Down
2 changes: 1 addition & 1 deletion src/InterfaxServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class InterfaxServiceProvider extends ServiceProvider
{
public function boot()
public function boot(): void
{
$this->app->when(InterfaxChannel::class)
->needs(Client::class)
Expand Down
23 changes: 23 additions & 0 deletions stubs/Resource.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Interfax
*
* (C) InterFAX, 2016
*
* @package interfax/interfax
* @author Interfax <[email protected]>
* @author Mike Smith <[email protected]>
* @copyright Copyright (c) 2016, InterFAX
* @license MIT
*/

namespace Interfax;

/**
* @property-read int $status
*/
abstract class Resource
{

}

0 comments on commit 9ec4ad5

Please sign in to comment.