diff --git a/.gitignore b/.gitignore index d07f69a..a2e3cbc 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ build composer.phar composer.lock .phpunit.result.cache +/.idea +/.vscode diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ae75d62..0000000 --- a/.travis.yml +++ /dev/null @@ -1,22 +0,0 @@ -language: php - -php: - - 7.2 - - 7.3 - - 7.4 - -env: - matrix: - - COMPOSER_FLAGS="--prefer-lowest" - - COMPOSER_FLAGS="" - -before_script: - - travis_retry composer self-update - - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source - -script: - - phpunit --coverage-text --coverage-clover=coverage.clover - -after_script: - - wget https://scrutinizer-ci.com/ocular.phar - - php ocular.phar code-coverage:upload --format=php-clover coverage.clover diff --git a/README.md b/README.md index 13d6c84..f230d30 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Pusher Beams push notifications channel for Laravel 5.5+, 6.x, 7.x & 8.x +# Pusher Beams push notifications channel for Laravel 8.x & 9.x [![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/pusher-push-notifications.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/pusher-push-notifications) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) @@ -17,7 +17,7 @@ Also please note that prior to version 2.0, this package integrated with Pusher' ## Contents -- [Pusher Beams push notifications channel for Laravel 5.5+, 6.x, 7.x & 8.x](#pusher-beams-push-notifications-channel-for-laravel-55-6x-7x--8x) +- [Pusher Beams push notifications channel for Laravel 8.x & 9.x](#pusher-beams-push-notifications-channel-for-laravel-55-6x-7x--8x) - [Contents](#contents) - [Installation](#installation) - [Setting up your Pusher account](#setting-up-your-pusher-account) @@ -38,7 +38,7 @@ Also please note that prior to version 2.0, this package integrated with Pusher' You can install the package via composer: -``` bash +```bash composer require laravel-notification-channels/pusher-push-notifications ``` @@ -51,7 +51,7 @@ Before using this package you should set up a Pusher Beams account. Here are the - Select your instance from the list or create a new instance. - Click on the "Settings" tab. - Upload your APNS Certificate and/or add your FCM Server key. -- Now select the "Credentials" tab. +- Now select the "Keys" tab. - Copy your `Instance Id`, and `Secret Key`. - Add a new entry to in your `config/services.php` file: ```php @@ -66,7 +66,7 @@ Before using this package you should set up a Pusher Beams account. Here are the Now you can use the channel in your `via()` method inside the Notification class. -``` php +```php use NotificationChannels\PusherPushNotifications\PusherChannel; use NotificationChannels\PusherPushNotifications\PusherMessage; use Illuminate\Notifications\Notification; @@ -132,18 +132,12 @@ public function toPushNotification($notifiable) By default, the pusher "interest" messages will be sent to will be defined using the {notifiable}.{id} convention, for example `App.User.1`, however you can change this behaviour by including a `routeNotificationFor()` in the notifiable class. -I.e. if you are pushing notification on ``User`` model, you can go to `App\User` class and implement method: +I.e. if you are pushing notification on `User` model, you can go to `App\Models\User` class and implement method: -``` -public function routeNotificationFor($channel) +```php +public function routeNotificationForPusherPushNotifications($notification): string { - if($channel === 'PusherPushNotifications'){ - return 'your.custom.interest.string'; - } - - $class = str_replace('\\', '.', get_class($this)); - - return $class.'.'.$this->getKey(); + return 'your.custom.interest.string'; } ``` @@ -168,7 +162,7 @@ Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recen ## Testing -``` bash +```bash $ composer test ``` diff --git a/composer.json b/composer.json index c0cb122..4cb8f4e 100644 --- a/composer.json +++ b/composer.json @@ -32,12 +32,12 @@ } ], "require": { - "php": ">=7.4", - "illuminate/events": "~7.0 || ~8.0 || ~9.0", - "illuminate/notifications": "~7.0 || ~8.0 || ~9.0", - "illuminate/queue": "~7.0 || ~8.0 || ~9.0", - "illuminate/support": "~7.0 || ~8.0 || ~9.0", - "pusher/pusher-push-notifications": "^1.1" + "php": ">=8.0", + "illuminate/events": "~8.0 || ~9.0", + "illuminate/notifications": "~8.0 || ~9.0", + "illuminate/queue": "~8.0 || ~9.0", + "illuminate/support": "~8.0 || ~9.0", + "pusher/pusher-push-notifications": "^2.0" }, "require-dev": { "mockery/mockery": "^1.3", diff --git a/src/Exceptions/CouldNotCreateMessage.php b/src/Exceptions/CouldNotCreateMessage.php index 3b8ec01..53abef6 100644 --- a/src/Exceptions/CouldNotCreateMessage.php +++ b/src/Exceptions/CouldNotCreateMessage.php @@ -7,19 +7,19 @@ class CouldNotCreateMessage extends Exception { /** - * @param string $platform + * @param string $platform * @return static */ - public static function invalidPlatformGiven($platform) + public static function invalidPlatformGiven(string $platform): static { return new static("Platform `{$platform}` is invalid. It should be either `iOS` or `Android`."); } /** - * @param $platform + * @param string $platform * @return static */ - public static function platformConflict($platform) + public static function platformConflict(string $platform): static { return new static("You are trying to send an extra message to `{$platform}` while the original message is to `{$platform}`."); } diff --git a/src/PusherChannel.php b/src/PusherChannel.php index f4ec8b5..f9216ba 100644 --- a/src/PusherChannel.php +++ b/src/PusherChannel.php @@ -16,14 +16,8 @@ class PusherChannel { public const INTERESTS = 'interests'; - protected PushNotifications $beamsClient; - - private Dispatcher $events; - - public function __construct(PushNotifications $beamsClient, Dispatcher $events) + public function __construct(protected PushNotifications $beamsClient, private Dispatcher $events) { - $this->beamsClient = $beamsClient; - $this->events = $events; } /** @@ -49,7 +43,7 @@ public function send($notifiable, Notification $notification): void ); } catch (Throwable $exception) { $this->events->dispatch( - new NotificationFailed($notifiable, $notification, 'pusher-push-notifications') + new NotificationFailed($notifiable, $notification, 'pusher-push-notifications', ['exception' => $exception]) ); } } @@ -60,7 +54,7 @@ public function send($notifiable, Notification $notification): void * @param mixed $notifiable * @return string */ - public static function defaultName($notifiable): string + public static function defaultName(mixed $notifiable): string { $class = str_replace('\\', '.', get_class($notifiable)); diff --git a/src/PusherMessage.php b/src/PusherMessage.php index f84338a..f9caef9 100644 --- a/src/PusherMessage.php +++ b/src/PusherMessage.php @@ -9,93 +9,71 @@ class PusherMessage { /** * The device platform (iOS/Android). - * - * @var string */ - protected $platform = 'iOS'; + protected string $platform = 'iOS'; /** * The message title. - * - * @var string */ - protected $title; - - /** - * The message body. - * - * @var string - */ - protected $body; + protected string|null $title = null; /** * The phone number the message should be sent from. - * - * @var string */ - protected $sound = 'default'; + protected string $sound = 'default'; /** * The message icon (Android). - * - * @var string */ - protected $icon; + protected string|null $icon = null; /** * The number to display next to the push notification (iOS). - * - * @var int */ - protected $badge; + protected int|null $badge = null; /** * URL to follow on notification click. */ - protected $link; + protected string|null $link = null; /** * Extra options that will get added to the message. - * - * @var array */ - protected $options = []; + protected array $options = []; /** * An extra message to the other platform. * * @var */ - protected $extraMessage; + protected PusherMessage|null $extraMessage = null; /** - * @param string $body - * - * @return static + * @param string $body */ - public static function create($body = '') + public function __construct(protected string $body = '') { - return new static($body); } /** - * @param string $body + * @param string $body + * @return static */ - public function __construct($body = '') + public static function create(string $body = ''): static { - $this->body = $body; + return new static($body); } /** * Set the platform [iOS/Android]. * - * @param string $platform - * + * @param string $platform * @return $this * * @throws \NotificationChannels\PusherPushNotifications\Exceptions\CouldNotCreateMessage */ - public function platform($platform) + public function platform(string $platform): self { if (! in_array($platform, ['iOS', 'Android', 'web'])) { throw CouldNotCreateMessage::invalidPlatformGiven($platform); @@ -111,7 +89,7 @@ public function platform($platform) * * @return $this */ - public function iOS() + public function iOS(): self { $this->platform = 'iOS'; @@ -123,7 +101,7 @@ public function iOS() * * @return $this */ - public function android() + public function android(): self { $this->platform = 'Android'; @@ -135,7 +113,7 @@ public function android() * * @return $this */ - public function web() + public function web(): self { $this->platform = 'web'; @@ -145,10 +123,12 @@ public function web() /** * Set an extra message to be sent to Android. * - * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message + * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message * @return $this + * + * @throws CouldNotCreateMessage */ - public function withAndroid(self $message) + public function withAndroid(self $message): self { $this->withExtra($message->android()); @@ -158,10 +138,12 @@ public function withAndroid(self $message) /** * Set an extra message to be sent to iOS. * - * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message + * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message * @return $this + * + * @throws CouldNotCreateMessage */ - public function withiOS(self $message) + public function withiOS(self $message): self { $this->withExtra($message->iOS()); @@ -171,10 +153,12 @@ public function withiOS(self $message) /** * Set an extra message to be sent to web. * - * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message + * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message * @return $this + * + * @throws CouldNotCreateMessage */ - public function withWeb(self $message) + public function withWeb(self $message): self { $this->withExtra($message->web()); @@ -184,12 +168,14 @@ public function withWeb(self $message) /** * Set an extra message to be sent to another platform. * - * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message + * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message * @return void + * + * @throws CouldNotCreateMessage */ - private function withExtra(self $message) + private function withExtra(self $message): void { - if ($message->getPlatform() == $this->platform) { + if ($message->getPlatform() === $this->platform) { throw CouldNotCreateMessage::platformConflict($this->platform); } @@ -199,11 +185,10 @@ private function withExtra(self $message) /** * Set the message title. * - * @param string $value - * + * @param string $value * @return $this */ - public function title($value) + public function title(string $value): self { $this->title = $value; @@ -213,11 +198,10 @@ public function title($value) /** * Set the message body. * - * @param string $value - * + * @param string $value * @return $this */ - public function body($value) + public function body(string $value): self { $this->body = $value; @@ -227,11 +211,10 @@ public function body($value) /** * Set the message sound (Android). * - * @param string $value - * + * @param string $value * @return $this */ - public function sound($value) + public function sound(string $value): self { $this->sound = $value; @@ -241,11 +224,10 @@ public function sound($value) /** * Set the message icon (Android). * - * @param string $value - * + * @param string $value * @return $this */ - public function icon($value) + public function icon(string $value): self { $this->icon = $value; @@ -255,13 +237,12 @@ public function icon($value) /** * Set the message badge (iOS). * - * @param int $value - * + * @param int $value * @return $this */ - public function badge($value) + public function badge(int $value): self { - $this->badge = (int) $value; + $this->badge = $value; return $this; } @@ -269,11 +250,10 @@ public function badge($value) /** * Set the message link. * - * @param string $value - * + * @param string $value * @return $this */ - public function link($value) + public function link(string $value): self { $this->link = $value; @@ -281,12 +261,11 @@ public function link($value) } /** - * @param string $key - * @param mixed $value - * + * @param string $key + * @param mixed $value * @return $this */ - public function setOption($key, $value) + public function setOption(string $key, mixed $value): self { $this->options[$key] = $value; @@ -298,16 +277,13 @@ public function setOption($key, $value) * * @return array */ - public function toArray() + public function toArray(): array { - switch ($this->platform) { - case 'Android': - return $this->toAndroid(); - case 'web': - return $this->toWeb(); - default: - return $this->toiOS(); - } + return match ($this->platform) { + 'Android' => $this->toAndroid(), + 'web' => $this->toWeb(), + default => $this->toiOS(), + }; } /** @@ -315,7 +291,7 @@ public function toArray() * * @return array */ - public function toiOS() + public function toiOS(): array { $message = [ 'apns' => [ @@ -340,7 +316,7 @@ public function toiOS() * * @return array */ - public function toAndroid() + public function toAndroid(): array { $message = [ 'fcm' => [ @@ -363,7 +339,7 @@ public function toAndroid() * * @return array */ - public function toWeb() + public function toWeb(): array { $message = [ 'web' => [ @@ -387,7 +363,7 @@ public function toWeb() * * @return string */ - public function getPlatform() + public function getPlatform(): string { return $this->platform; } @@ -397,7 +373,7 @@ public function getPlatform() * * @param $message */ - private function formatMessage(&$message) + private function formatMessage(&$message): void { if ($this->extraMessage) { $message = array_merge($message, $this->extraMessage->toArray()); diff --git a/src/PusherPushNotificationsServiceProvider.php b/src/PusherPushNotificationsServiceProvider.php index 5ebab79..72a133b 100644 --- a/src/PusherPushNotificationsServiceProvider.php +++ b/src/PusherPushNotificationsServiceProvider.php @@ -10,7 +10,7 @@ class PusherPushNotificationsServiceProvider extends ServiceProvider /** * Bootstrap the application services. */ - public function boot() + public function boot(): void { $this->app->when(PusherChannel::class) ->needs(PushNotifications::class) diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 31159fc..954eed5 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -33,7 +33,7 @@ public function setUp(): void } /** @test */ - public function it_can_send_a_notification_to_interest() + public function it_can_send_a_notification_to_interest(): void { $message = $this->notification->toPushNotification($this->notifiableInterest); @@ -45,7 +45,7 @@ public function it_can_send_a_notification_to_interest() } /** @test */ - public function it_can_send_a_notification_to_interests() + public function it_can_send_a_notification_to_interests(): void { $message = $this->notification->toPushNotification($this->notifiableInterests); @@ -59,7 +59,7 @@ public function it_can_send_a_notification_to_interests() } /** @test */ - public function it_fires_failure_event_on_interest_failure() + public function it_fires_failure_event_on_interest_failure(): void { $message = $this->notification->toPushNotification($this->notifiableInterest); @@ -73,7 +73,7 @@ public function it_fires_failure_event_on_interest_failure() } /** @test */ - public function it_can_send_a_notification_to_user() + public function it_can_send_a_notification_to_user(): void { $message = $this->notification->toPushNotification($this->notifiableUser); @@ -85,7 +85,7 @@ public function it_can_send_a_notification_to_user() } /** @test */ - public function it_can_send_a_notification_to_users() + public function it_can_send_a_notification_to_users(): void { $message = $this->notification->toPushNotification($this->notifiableUsers); @@ -99,7 +99,7 @@ public function it_can_send_a_notification_to_users() } /** @test */ - public function it_fires_failure_event_on_user_failure() + public function it_fires_failure_event_on_user_failure(): void { $message = $this->notification->toPushNotification($this->notifiableUser); @@ -117,7 +117,7 @@ class TestNotifiableInterest { use Notifiable; - public function routeNotificationForPusherPushNotifications() + public function routeNotificationForPusherPushNotifications(): string { return 'interest_name'; } @@ -127,7 +127,7 @@ class TestNotifiableInterests { use Notifiable; - public function routeNotificationForPusherPushNotifications() + public function routeNotificationForPusherPushNotifications(): array { return ['interest_one', 'interest_two', 'interest_three']; } @@ -137,9 +137,9 @@ class TestNotifiableUser { use Notifiable; - public $pushNotificationType = 'users'; + public string $pushNotificationType = 'users'; - public function routeNotificationForPusherPushNotifications() + public function routeNotificationForPusherPushNotifications(): string { return 'user_1'; } @@ -149,9 +149,9 @@ class TestNotifiableUsers { use Notifiable; - public $pushNotificationType = 'users'; + public string $pushNotificationType = 'users'; - public function routeNotificationForPusherPushNotifications() + public function routeNotificationForPusherPushNotifications(): array { return ['user_1', 'user_2', 'user_3']; } @@ -159,7 +159,7 @@ public function routeNotificationForPusherPushNotifications() class TestNotification extends Notification { - public function toPushNotification($notifiable) + public function toPushNotification($notifiable): PusherMessage { return new PusherMessage(); } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 6cde9f0..fe223d0 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -10,7 +10,7 @@ class MessageTest extends MockeryTestCase { /** @var \NotificationChannels\PusherPushNotifications\PusherMessage */ - protected $message; + protected PusherMessage $message; public function setUp(): void { @@ -20,7 +20,7 @@ public function setUp(): void } /** @test */ - public function it_can_accept_a_message_when_constructing_a_message() + public function it_can_accept_a_message_when_constructing_a_message(): void { $message = new PusherMessage('myMessage'); @@ -28,7 +28,7 @@ public function it_can_accept_a_message_when_constructing_a_message() } /** @test */ - public function it_provides_a_create_method() + public function it_provides_a_create_method(): void { $message = PusherMessage::create('myMessage'); @@ -36,14 +36,14 @@ public function it_provides_a_create_method() } /** @test */ - public function by_default_it_will_send_a_message_to_ios() + public function by_default_it_will_send_a_message_to_ios(): void { $this->assertTrue(Arr::has($this->message->toArray(), 'apns')); $this->assertFalse(Arr::has($this->message->toArray(), 'fcm')); } /** @test */ - public function it_can_send_a_message_to_the_right_platform() + public function it_can_send_a_message_to_the_right_platform(): void { $this->message->ios(); @@ -57,13 +57,13 @@ public function it_can_send_a_message_to_the_right_platform() } /** @test */ - public function it_sets_a_default_sound() + public function it_sets_a_default_sound(): void { $this->assertEquals('default', Arr::get($this->message->toArray(), 'apns.aps.sound')); } /** @test */ - public function it_can_set_the_title() + public function it_can_set_the_title(): void { $this->message->title('myTitle'); @@ -73,7 +73,7 @@ public function it_can_set_the_title() } /** @test */ - public function it_can_set_the_body() + public function it_can_set_the_body(): void { $this->message->body('myBody'); @@ -83,7 +83,7 @@ public function it_can_set_the_body() } /** @test */ - public function it_can_set_the_sound() + public function it_can_set_the_sound(): void { $this->message->sound('mySound'); @@ -93,7 +93,7 @@ public function it_can_set_the_sound() } /** @test */ - public function it_can_set_the_badge() + public function it_can_set_the_badge(): void { $this->message->badge(5); @@ -101,7 +101,7 @@ public function it_can_set_the_badge() } /** @test */ - public function it_can_set_the_icon() + public function it_can_set_the_icon(): void { $this->message->icon('myIcon'); @@ -109,7 +109,7 @@ public function it_can_set_the_icon() } /** @test */ - public function it_will_throw_an_exception_when_an_unsupported_platform_is_used() + public function it_will_throw_an_exception_when_an_unsupported_platform_is_used(): void { $this->expectException(CouldNotCreateMessage::class); @@ -117,7 +117,7 @@ public function it_will_throw_an_exception_when_an_unsupported_platform_is_used( } /** @test */ - public function it_can_send_message_to_multiple_platforms() + public function it_can_send_message_to_multiple_platforms(): void { $this->message->ios()->withAndroid(new PusherMessage()); diff --git a/tests/Notifiable.php b/tests/Notifiable.php index 8d3e369..1428eae 100644 --- a/tests/Notifiable.php +++ b/tests/Notifiable.php @@ -9,7 +9,7 @@ class Notifiable /** * @return int */ - public function getKey() + public function getKey(): int { return 1; }