├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── pint.json └── src ├── NotificationEventSubscriber.php └── NotificationEventSubscriberServiceProvider.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-notification-event-subscriber` will be documented in this file. 4 | 5 | ## 1.4.0 - 2024-12-18 6 | - Added Laravel 11 and PHP 8.3 supports. 7 | 8 | ## 1.3.0 - 2023-02-19 9 | - Added Laravel 10 & php 8.2 support. 10 | 11 | ## 1.2.0 - 2022-09-27 12 | - Added `$notifiable` parameter of the `NotificationSent` and `NotificationSending` events to `onSent()` and `enSending()` methods. 13 | 14 | ## 1.1.0 - 2022-09-21 15 | - Added `$response` parameter of the `NotificationSent` event to `onSent()` method. 16 | 17 | ## 1.0.0 - 2022-09-09 18 | - initial release. 19 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) tkaratug 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![image](https://banners.beyondco.de/Laravel%20Notification%20Event%20Subscriber.png?theme=light&packageManager=composer+require&packageName=tkaratug%2Flaravel-notification-event-subscriber&pattern=architect&style=style_1&description=&md=1&showWatermark=0&fontSize=100px&images=bell&widths=200&heights=200) 2 | 3 | # Laravel Notification Event Subscriber 4 | 5 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/tkaratug/laravel-notification-event-subscriber.svg?style=flat-square)](https://packagist.org/packages/tkaratug/laravel-notification-event-subscriber) 6 | [![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/tkaratug/laravel-notification-event-subscriber/run-tests?label=tests)](https://github.com/tkaratug/laravel-notification-event-subscriber/actions?query=workflow%3Arun-tests+branch%3Amain) 7 | [![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/tkaratug/laravel-notification-event-subscriber/Fix%20PHP%20code%20style%20issues?label=code%20style)](https://github.com/tkaratug/laravel-notification-event-subscriber/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) 8 | [![Total Downloads](https://img.shields.io/packagist/dt/tkaratug/laravel-notification-event-subscriber.svg?style=flat-square)](https://packagist.org/packages/tkaratug/laravel-notification-event-subscriber) 9 | 10 | This package allows you to run any kind of actions while a notification is being sent or after it has been sent using `onSent()` and `onSending()` methods. 11 | 12 | It registers an event subscriber `NotificationEventSubscriber` and listens to the `NotificationSent` and `NotificationSending` events of Laravel. 13 | When one of them is fired, the event subscriber runs a defined method according to the event. 14 | 15 | ## Installation 16 | 17 | You can install the package via composer: 18 | 19 | ```bash 20 | composer require tkaratug/laravel-notification-event-subscriber 21 | ``` 22 | 23 | ## Usage 24 | 25 | ```php 26 | namespace App\Notifications; 27 | 28 | use Illuminate\Notifications\Messages\MailMessage; 29 | use Illuminate\Support\Facades\Log; 30 | 31 | class UserRegisteredNotification extends Notification 32 | { 33 | public function via($notifiable): array 34 | { 35 | return ['mail']; 36 | } 37 | 38 | public function toMail($notifiable): MailMessage 39 | { 40 | return (new MailMessage) 41 | ->greeting('foo') 42 | ->line('bar'); 43 | } 44 | 45 | public function onSending($notifiable, $channel, $response = null): void 46 | { 47 | Log::info($this::class . ' is being sent to via ' . $channel); 48 | } 49 | 50 | public function onSent($notifiable, $channel): void 51 | { 52 | Log::info($this::class . ' has been sent to via ' . $channel); 53 | } 54 | } 55 | ``` 56 | 57 | ## Testing 58 | 59 | ```bash 60 | composer test 61 | ``` 62 | 63 | ## Changelog 64 | 65 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 66 | 67 | ## Contributing 68 | 69 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 70 | 71 | ## Security Vulnerabilities 72 | 73 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 74 | 75 | ## Credits 76 | 77 | - [Turan Karatuğ](https://github.com/tkaratug) 78 | - [All Contributors](../../contributors) 79 | 80 | ## License 81 | 82 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 83 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tkaratug/laravel-notification-event-subscriber", 3 | "description": "This is my package laravel-notification-event-subscriber", 4 | "keywords": [ 5 | "tkaratug", 6 | "laravel", 7 | "laravel-notification-event-subscriber" 8 | ], 9 | "homepage": "https://github.com/tkaratug/laravel-notification-event-subscriber", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Turan Karatuğ", 14 | "email": "tkaratug@hotmail.com.tr", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.1 | ^8.2 | ^8.3", 20 | "illuminate/contracts": "^9.0 | ^10.0 | ^11.0" 21 | }, 22 | "require-dev": { 23 | "laravel/pint": "^1.0", 24 | "orchestra/testbench": "^7.0 | ^8.0", 25 | "phpunit/phpunit": "^9.5" 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "Tkaratug\\NotificationEventSubscriber\\": "src" 30 | } 31 | }, 32 | "autoload-dev": { 33 | "psr-4": { 34 | "Tkaratug\\NotificationEventSubscriber\\Tests\\": "tests" 35 | } 36 | }, 37 | "scripts": { 38 | "test": "vendor/bin/phpunit", 39 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage", 40 | "format": "vendor/bin/pint" 41 | }, 42 | "config": { 43 | "sort-packages": true 44 | }, 45 | "extra": { 46 | "laravel": { 47 | "providers": [ 48 | "Tkaratug\\NotificationEventSubscriber\\NotificationEventSubscriberServiceProvider" 49 | ] 50 | } 51 | }, 52 | "minimum-stability": "dev", 53 | "prefer-stable": true 54 | } 55 | -------------------------------------------------------------------------------- /pint.json: -------------------------------------------------------------------------------- 1 | { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /src/NotificationEventSubscriber.php: -------------------------------------------------------------------------------- 1 | notification, 'onSent')) { 19 | $event->notification->onSent($event->notifiable, $event->channel, $event->response); 20 | } 21 | } 22 | 23 | /** 24 | * Executes `onSending()` method in the notification. 25 | */ 26 | public function handleNotificationSending(NotificationSending $event): void 27 | { 28 | if (method_exists($event->notification, 'onSending')) { 29 | $event->notification->onSending($event->notifiable, $event->channel); 30 | } 31 | } 32 | 33 | /** 34 | * The subscriber classes to register. 35 | */ 36 | public function subscribe(Dispatcher $events): array 37 | { 38 | return [ 39 | NotificationSent::class => 'handleNotificationSent', 40 | NotificationSending::class => 'handleNotificationSending', 41 | ]; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/NotificationEventSubscriberServiceProvider.php: -------------------------------------------------------------------------------- 1 |