From 52ddc8288193bea00ab65755cdde90acc621cae8 Mon Sep 17 00:00:00 2001 From: cengiz Date: Tue, 2 Jan 2024 17:04:00 +0200 Subject: [PATCH 1/9] init --- .editorconfig | 15 ++++ .github/workflows/tests.yml | 32 +++++++++ .gitignore | 2 + .styleci.yml | 1 + CHANGELOG.md | 7 ++ CONTRIBUTING.md | 55 ++++++++++++++ LICENSE.md | 21 ++++++ README.md | 80 +++++++++++++++++---- composer.json | 40 +++++++++++ src/Exceptions/CouldNotSendNotification.php | 11 +++ src/Ntfy.php | 53 ++++++++++++++ src/NtfyChannel.php | 34 +++++++++ src/NtfyMessage.php | 44 ++++++++++++ src/NtfyServiceProvider.php | 28 ++++++++ 14 files changed, 411 insertions(+), 12 deletions(-) create mode 100644 .editorconfig create mode 100644 .github/workflows/tests.yml create mode 100644 .gitignore create mode 100644 .styleci.yml create mode 100644 CHANGELOG.md create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE.md create mode 100644 composer.json create mode 100644 src/Exceptions/CouldNotSendNotification.php create mode 100644 src/Ntfy.php create mode 100644 src/NtfyChannel.php create mode 100644 src/NtfyMessage.php create mode 100644 src/NtfyServiceProvider.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..cd8eb86e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at http://editorconfig.org + +root = true + +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 00000000..f9ff88d2 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,32 @@ +name: PHPUnit tests + +on: + - push + - pull_request + +jobs: + tests: + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + php: [7.2, 7.3, 7.4, 8.0] + + name: Tests on PHP ${{ matrix.php }} - ${{ matrix.stability }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer:v2 + coverage: none + + - name: Install dependencies + run: composer update --prefer-source --no-interaction --no-progress + + - name: Execute tests + run: vendor/bin/phpunit --verbose diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..029f7c80 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +composer.lock diff --git a/.styleci.yml b/.styleci.yml new file mode 100644 index 00000000..0285f179 --- /dev/null +++ b/.styleci.yml @@ -0,0 +1 @@ +preset: laravel diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..6f296c23 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +# Changelog + +All notable changes to `ntfy` will be documented in this file + +## 1.0.0 - 201X-XX-XX + +- initial release diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..4da74e3f --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,55 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. + +Please read and understand the contribution guide before creating an issue or pull request. + +## Etiquette + +This project is open source, and as such, the maintainers give their free time to build and maintain the source code +held within. They make the code freely available in the hope that it will be of use to other developers. It would be +extremely unfair for them to suffer abuse or anger for their hard work. + +Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the +world that developers are civilized and selfless people. + +It's the duty of the maintainer to ensure that all submissions to the project are of sufficient +quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. + +## Viability + +When requesting or submitting new features, first consider whether it might be useful to others. Open +source projects are used by many developers, who may have entirely different needs to your own. Think about +whether or not your feature is likely to be used by other users of the project. + +## Procedure + +Before filing an issue: + +- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. +- Check to make sure your feature suggestion isn't already present within the project. +- Check the pull requests tab to ensure that the bug doesn't have a fix in progress. +- Check the pull requests tab to ensure that the feature isn't already in progress. + +Before submitting a pull request: + +- Check the codebase to ensure that your feature doesn't already exist. +- Check the pull requests to ensure that another person hasn't already submitted the feature or fix. + +## Requirements + +If the project maintainer has any additional requirements, you will find them listed here. + +- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). + +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. + +**Happy coding**! diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 00000000..9acbafd1 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# The MIT License (MIT) + +Copyright (c) Cengiz Onkal + +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. diff --git a/README.md b/README.md index fa8362b7..125a0c03 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,73 @@ -# New Notification Channels +Please see [this repo](https://github.com/laravel-notification-channels/channels) for instructions on how to submit a channel proposal. -### Suggesting a new channel -Have a suggestion or working on a new channel? Please create a new issue for that service. +# Ntfy -### I'm working on a new channel -Please create an issue for it if it does not already exist, then PR you code for review. +This package makes it easy to send notifications using [ntfy](link to service) with Laravel 5.5+, 6.x and 7.x -## Workflow for new channels +## Contents -1) Head over to the [skeleton repo](https://github.com/laravel-notification-channels/skeleton) download a ZIP copy. This is important, to ensure you start from a fresh commit history. -2) Use find/replace to replace all of the placeholders with the correct values (package name, author name, email, etc). -3) Implement to logic for the channel & add tests. -4) Fork this repo, add it as a remote and push your new channel to a branch. -5) Submit a new PR against this repo for review. +- [Installation](#installation) + - [Setting up the ntfy service](#setting-up-the-ntfy-service) +- [Usage](#usage) + - [Available Message methods](#available-message-methods) +- [Changelog](#changelog) +- [Testing](#testing) +- [Security](#security) +- [Contributing](#contributing) +- [Credits](#credits) +- [License](#license) -Take a look at our [FAQ](http://laravel-notification-channels.com/) to see our small list of rules, to provide top-notch notification channels. + +## Installation + +You can install the package via composer: + +```bash +composer require laravel-notification-channels/ntfy +``` +```php +// config/services.php +'providers' => [ + // ... + NotificationChannels\Ntfy\NtfyServiceProvider::class, +], +``` + +### Setting up the ntfy service + +Optionally include a few steps how users can set up the service. + +## Usage + +Some code examples, make it clear how to use the package + +### Available Message methods + +A list of all available options + +## Changelog + +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. + +## Testing + +``` bash +$ composer test +``` + +## Security + +If you discover any security related issues, please email onkal.cengiz@gmail.com instead of using the issue tracker. + +## Contributing + +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. + +## Credits + +- [Cengiz Onkal](https://github.com/cengizonkal) +- [All Contributors](../../contributors) + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..65dde14d --- /dev/null +++ b/composer.json @@ -0,0 +1,40 @@ +{ + "name": "laravel-notification-channels/ntfy", + "description": "Laravel notification channel for Ntfy", + "homepage": "https://github.com/laravel-notification-channels/ntfy", + "license": "MIT", + "authors": [ + { + "name": "Cengiz Onkal", + "email": "onkal.cengiz@gmail.com", + "homepage": "https://github.com/cengizonkal", + "role": "Developer" + } + ], + "require": { + "php": ">=7.2", + "illuminate/notifications": "~6.0 || ~7.0 || ~8.0 || ^9.0 || ^10.0", + "illuminate/support": "~6.0 || ~7.0 || ~8.0 || ^9.0 || ^10.0" + }, + "require-dev": { + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^9.0" + }, + "autoload": { + "psr-4": { + "NotificationChannels\\Ntfy\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "NotificationChannels\\Ntfy\\Test\\": "tests" + } + }, + "scripts": { + "test": "phpunit", + "test:coverage": "phpunit --coverage-text --coverage-clover=coverage.clover" + }, + "config": { + "sort-packages": true + } +} diff --git a/src/Exceptions/CouldNotSendNotification.php b/src/Exceptions/CouldNotSendNotification.php new file mode 100644 index 00000000..02084155 --- /dev/null +++ b/src/Exceptions/CouldNotSendNotification.php @@ -0,0 +1,11 @@ +host = $host; + $this->username = $username; + $this->password = $password; + $this->client = $client; + $this->port = $port; + } + + public function send(NtfyMessage $message) + { + try { + + $response = $this->client->request('POST', $this->host.':'.$this->port . '/' . $message->topic, [ + 'auth' => [$this->username, $this->password], + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'json' => [ + "message" => $message->content, + "title" => $message->title, + "priority" => $message->priority, + + ], + ]); + + + } catch (\Exception $e) { + throw new CouldNotSendNotification($e->getMessage()); + } + + return $response; + } + + +} diff --git a/src/NtfyChannel.php b/src/NtfyChannel.php new file mode 100644 index 00000000..2adb082c --- /dev/null +++ b/src/NtfyChannel.php @@ -0,0 +1,34 @@ +service = $service; + } + + /** + * Send the given notification. + * + * @param mixed $notifiable + * @param \Illuminate\Notifications\Notification $notification + * + * @throws \NotificationChannels\Ntfy\Exceptions\CouldNotSendNotification + */ + public function send($notifiable, Notification $notification) + { + //$response = [a call to the api of your notification send] + $this->service->send($notification->toNtfy($notifiable)); + +// if ($response->error) { // replace this by the code need to check for errors +// throw CouldNotSendNotification::serviceRespondedWithAnError($response); +// } + } +} diff --git a/src/NtfyMessage.php b/src/NtfyMessage.php new file mode 100644 index 00000000..20ae5e39 --- /dev/null +++ b/src/NtfyMessage.php @@ -0,0 +1,44 @@ +title = $title; + return $this; + } + + public function content(string $content) + { + $this->content = $content; + return $this; + } + + public function priority(int $priority) + { + $this->priority = $priority; + return $this; + } + + public function body(string $body) + { + return $this->content($body); + } + + public function topic(string $topic) + { + $this->topic = $topic; + return $this; + } + + +} diff --git a/src/NtfyServiceProvider.php b/src/NtfyServiceProvider.php new file mode 100644 index 00000000..eda9db2f --- /dev/null +++ b/src/NtfyServiceProvider.php @@ -0,0 +1,28 @@ +app->when(NtfyChannel::class) + ->needs(Ntfy::class) + ->give(function () { + $ntfyConfig = config('broadcasting.connections.ntfy'); + return new Ntfy( + $ntfyConfig['host'], + $ntfyConfig['port'], + $ntfyConfig['username'], + $ntfyConfig['password'], + new \GuzzleHttp\Client() + ); + }); + } + +} From aea7864c528dc917fd1226dafee5a0c69b86bc0e Mon Sep 17 00:00:00 2001 From: cengiz Date: Thu, 4 Jan 2024 09:37:51 +0200 Subject: [PATCH 2/9] wip --- .gitignore | 1 + README.md | 54 +++++++++++++++++++++++++++++---------------- composer.json | 3 ++- src/Ntfy.php | 16 +++++++------- src/NtfyMessage.php | 22 +++++++++++++----- 5 files changed, 63 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 029f7c80..2942141e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea/ composer.lock +vendor/ diff --git a/README.md b/README.md index 125a0c03..8b449d1e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Please see [this repo](https://github.com/laravel-notification-channels/channels # Ntfy -This package makes it easy to send notifications using [ntfy](link to service) with Laravel 5.5+, 6.x and 7.x +This package makes it easy to send notifications using [ntfy](https://ntfy.sh/) with Laravel 5.5+, 6.x and 7.x ## Contents @@ -10,10 +10,7 @@ This package makes it easy to send notifications using [ntfy](link to service) w - [Setting up the ntfy service](#setting-up-the-ntfy-service) - [Usage](#usage) - [Available Message methods](#available-message-methods) -- [Changelog](#changelog) - [Testing](#testing) -- [Security](#security) -- [Contributing](#contributing) - [Credits](#credits) - [License](#license) @@ -35,19 +32,47 @@ composer require laravel-notification-channels/ntfy ### Setting up the ntfy service -Optionally include a few steps how users can set up the service. +Configure ntft service in `config/boardcasting.php`: +```php + 'ntfy' => [ + 'driver' => 'ntfy', + 'host' => env('NTFY_HOST'), + 'port' => env('NTFY_PORT'), + 'username' => env('NTFY_USERNAME'), + 'password' => env('NTFY_PASSWORD'), + ], +``` ## Usage -Some code examples, make it clear how to use the package +Now you can use the channel in your `via()` method inside the notification: -### Available Message methods +```php + public function via(object $notifiable): array + { + return [NtfyChannel::class]; + } + + /** + * Get the mail representation of the notification. + */ + public function toNtfy(object $notifiable) + { + return (new NtfyMessage()) + ->topic('test') + ->title('Test Notification') + ->body('This is a test notification from Ntfy'); + } +``` -A list of all available options +### Available Message methods -## Changelog +- `topic('')`: Accepts a string value for the topic of the notification. +- `title('')`: Accepts a string value for the title of the notification. +- `body('')`: Accepts a string value for the body of the notification. +- `priority('')`: Accepts a string value for the priority of the notification.(1=Min priority, 2=Low priority, 3=Default priority, 4=High priority, 5=Max priority) +- -Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. ## Testing @@ -55,18 +80,9 @@ Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recen $ composer test ``` -## Security - -If you discover any security related issues, please email onkal.cengiz@gmail.com instead of using the issue tracker. - -## Contributing - -Please see [CONTRIBUTING](CONTRIBUTING.md) for details. - ## Credits - [Cengiz Onkal](https://github.com/cengizonkal) -- [All Contributors](../../contributors) ## License diff --git a/composer.json b/composer.json index 65dde14d..e9396616 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "require": { "php": ">=7.2", "illuminate/notifications": "~6.0 || ~7.0 || ~8.0 || ^9.0 || ^10.0", - "illuminate/support": "~6.0 || ~7.0 || ~8.0 || ^9.0 || ^10.0" + "illuminate/support": "~6.0 || ~7.0 || ~8.0 || ^9.0 || ^10.0", + "psr/http-client": "^1.0" }, "require-dev": { "mockery/mockery": "^1.0", diff --git a/src/Ntfy.php b/src/Ntfy.php index f27ab4b2..4aac2739 100644 --- a/src/Ntfy.php +++ b/src/Ntfy.php @@ -3,7 +3,6 @@ namespace NotificationChannels\Ntfy; -use GuzzleHttp\Client; use NotificationChannels\Ntfy\Exceptions\CouldNotSendNotification; class Ntfy @@ -27,27 +26,28 @@ public function __construct($host, $port, $username, $password, \Psr\Http\Client public function send(NtfyMessage $message) { try { - - $response = $this->client->request('POST', $this->host.':'.$this->port . '/' . $message->topic, [ + $request = new \GuzzleHttp\Psr7\Request('POST', $this->host . ':' . $this->port, [ 'auth' => [$this->username, $this->password], 'headers' => [ 'Content-Type' => 'application/json', ], 'json' => [ + "topic" => $message->topic, "message" => $message->content, "title" => $message->title, "priority" => $message->priority, - + "tags" => [], + "attach" => "", + "filename" => "", + "click" => "", + "actions" => [], ], ]); - - + $response = $this->client->sendRequest($request); } catch (\Exception $e) { throw new CouldNotSendNotification($e->getMessage()); } - return $response; } - } diff --git a/src/NtfyMessage.php b/src/NtfyMessage.php index 20ae5e39..e9c175fc 100644 --- a/src/NtfyMessage.php +++ b/src/NtfyMessage.php @@ -10,35 +10,47 @@ class NtfyMessage public $content; public $priority = 3; public $topic; + public $tags = []; + public $actions = []; + public $attach; + public $filename; - public function title(string $title) + public function title(string $title): NtfyMessage { $this->title = $title; return $this; } - public function content(string $content) + public function content(string $content): NtfyMessage { $this->content = $content; return $this; } - public function priority(int $priority) + public function priority(int $priority): NtfyMessage { $this->priority = $priority; return $this; } - public function body(string $body) + public function body(string $body): NtfyMessage { return $this->content($body); } - public function topic(string $topic) + public function topic(string $topic): NtfyMessage { $this->topic = $topic; return $this; } + public function actions($actions): NtfyMessage + { + $this->actions = $actions; + return $this; + } + + + } From d5ed198a462589a53a5d752e7cafcd4d55dd6d15 Mon Sep 17 00:00:00 2001 From: cengiz Date: Fri, 5 Jan 2024 16:26:25 +0200 Subject: [PATCH 3/9] added tests --- .github/workflows/tests.yml | 2 +- composer.json | 6 +-- src/Exceptions/CouldNotSendNotification.php | 5 ++ src/NtfyChannel.php | 13 +++--- tests/NtfyChannelTest.php | 20 ++++++++ tests/NtfyMessageTest.php | 51 +++++++++++++++++++++ 6 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 tests/NtfyChannelTest.php create mode 100644 tests/NtfyMessageTest.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f9ff88d2..62534ee0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -10,7 +10,7 @@ jobs: strategy: fail-fast: true matrix: - php: [7.2, 7.3, 7.4, 8.0] + php: [8.2,8.1] name: Tests on PHP ${{ matrix.php }} - ${{ matrix.stability }} diff --git a/composer.json b/composer.json index e9396616..f76635cc 100644 --- a/composer.json +++ b/composer.json @@ -12,9 +12,9 @@ } ], "require": { - "php": ">=7.2", - "illuminate/notifications": "~6.0 || ~7.0 || ~8.0 || ^9.0 || ^10.0", - "illuminate/support": "~6.0 || ~7.0 || ~8.0 || ^9.0 || ^10.0", + "php": ">=8.1", + "illuminate/notifications": "^10.0", + "illuminate/support": "^10.0", "psr/http-client": "^1.0" }, "require-dev": { diff --git a/src/Exceptions/CouldNotSendNotification.php b/src/Exceptions/CouldNotSendNotification.php index 02084155..feaf94e2 100644 --- a/src/Exceptions/CouldNotSendNotification.php +++ b/src/Exceptions/CouldNotSendNotification.php @@ -8,4 +8,9 @@ public static function serviceRespondedWithAnError($response) { return new static($response); } + + public static function missingNtfyMethod() + { + return new static('Notification is missing toNtfy() method.'); + } } diff --git a/src/NtfyChannel.php b/src/NtfyChannel.php index 2adb082c..33a0163c 100644 --- a/src/NtfyChannel.php +++ b/src/NtfyChannel.php @@ -8,12 +8,12 @@ class NtfyChannel { private $service; - + public function __construct(Ntfy $service) { $this->service = $service; } - + /** * Send the given notification. * @@ -24,11 +24,10 @@ public function __construct(Ntfy $service) */ public function send($notifiable, Notification $notification) { - //$response = [a call to the api of your notification send] + //make sure that the $notification object has a toNtfy() method + if (!method_exists($notification, 'toNtfy')) { + throw CouldNotSendNotification::missingNtfyMethod(); + } $this->service->send($notification->toNtfy($notifiable)); - -// if ($response->error) { // replace this by the code need to check for errors -// throw CouldNotSendNotification::serviceRespondedWithAnError($response); -// } } } diff --git a/tests/NtfyChannelTest.php b/tests/NtfyChannelTest.php new file mode 100644 index 00000000..1088ecd8 --- /dev/null +++ b/tests/NtfyChannelTest.php @@ -0,0 +1,20 @@ +expectException(\NotificationChannels\Ntfy\Exceptions\CouldNotSendNotification::class); + $client = $this->getMockBuilder(\Psr\Http\Client\ClientInterface::class)->getMock(); + $channel = new NtfyChannel(new \NotificationChannels\Ntfy\Ntfy('host', 'port', 'username', 'password', $client)); + $notification = $this->getMockBuilder(\Illuminate\Notifications\Notification::class)->getMock(); + $channel->send(new \stdClass(), $notification); + } + +} diff --git a/tests/NtfyMessageTest.php b/tests/NtfyMessageTest.php new file mode 100644 index 00000000..92939f26 --- /dev/null +++ b/tests/NtfyMessageTest.php @@ -0,0 +1,51 @@ +body('body'); + $this->assertEquals('body', $message->content); + } + + public function testActions() + { + $message = new NtfyMessage(); + $message->actions(['action1', 'action2']); + $this->assertEquals(['action1', 'action2'], $message->actions); + } + + public function testContent() + { + $message = new NtfyMessage(); + $message->content('content'); + $this->assertEquals('content', $message->content); + } + + public function testTopic() + { + $message = new NtfyMessage(); + $message->topic('topic'); + $this->assertEquals('topic', $message->topic); + } + + public function testPriority() + { + $message = new NtfyMessage(); + $message->priority(1); + $this->assertEquals(1, $message->priority); + } + + public function testTitle() + { + $message = new NtfyMessage(); + $message->title('title'); + $this->assertEquals('title', $message->title); + } +} From ecd269d7c2651a01a747e07a3c130ff528053bfe Mon Sep 17 00:00:00 2001 From: cengiz Date: Fri, 5 Jan 2024 16:27:40 +0200 Subject: [PATCH 4/9] readme fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8b449d1e..66cca30e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Please see [this repo](https://github.com/laravel-notification-channels/channels # Ntfy -This package makes it easy to send notifications using [ntfy](https://ntfy.sh/) with Laravel 5.5+, 6.x and 7.x +This package makes it easy to send notifications using [ntfy](https://ntfy.sh/) with Laravel 10 ## Contents From 4bf52b6065b0606262778234c8d436568e05e82c Mon Sep 17 00:00:00 2001 From: cengiz Date: Fri, 5 Jan 2024 16:36:45 +0200 Subject: [PATCH 5/9] github actions fix --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 62534ee0..0a2438ac 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -29,4 +29,4 @@ jobs: run: composer update --prefer-source --no-interaction --no-progress - name: Execute tests - run: vendor/bin/phpunit --verbose + run: composer test tests From b2840a8259927190b0921514dbaec70e9ef1b165 Mon Sep 17 00:00:00 2001 From: cengiz Date: Fri, 5 Jan 2024 17:09:11 +0200 Subject: [PATCH 6/9] guzzle http --- composer.json | 4 ++-- src/Ntfy.php | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index f76635cc..7e4d9a91 100644 --- a/composer.json +++ b/composer.json @@ -13,9 +13,9 @@ ], "require": { "php": ">=8.1", + "guzzle/http": "^3.9", "illuminate/notifications": "^10.0", - "illuminate/support": "^10.0", - "psr/http-client": "^1.0" + "illuminate/support": "^10.0" }, "require-dev": { "mockery/mockery": "^1.0", diff --git a/src/Ntfy.php b/src/Ntfy.php index 4aac2739..a1f0b958 100644 --- a/src/Ntfy.php +++ b/src/Ntfy.php @@ -3,6 +3,7 @@ namespace NotificationChannels\Ntfy; +use GuzzleHttp\Psr7\Request; use NotificationChannels\Ntfy\Exceptions\CouldNotSendNotification; class Ntfy @@ -14,22 +15,23 @@ class Ntfy private $client; private $port; - public function __construct($host, $port, $username, $password, \Psr\Http\Client\ClientInterface $client) + public function __construct($host, $port, $username, $password) { $this->host = $host; $this->username = $username; $this->password = $password; - $this->client = $client; + $this->client = new \GuzzleHttp\Client(); $this->port = $port; } public function send(NtfyMessage $message) { try { - $request = new \GuzzleHttp\Psr7\Request('POST', $this->host . ':' . $this->port, [ + $response = $this->client->post($this->host . ':' . $this->port, [ 'auth' => [$this->username, $this->password], 'headers' => [ 'Content-Type' => 'application/json', + 'Accept' => 'application/json', ], 'json' => [ "topic" => $message->topic, @@ -43,9 +45,8 @@ public function send(NtfyMessage $message) "actions" => [], ], ]); - $response = $this->client->sendRequest($request); } catch (\Exception $e) { - throw new CouldNotSendNotification($e->getMessage()); + throw new CouldNotSendNotification($e); } return $response; } From cafcc78f043422c6350d826e3ffee2f0956178f6 Mon Sep 17 00:00:00 2001 From: cengiz Date: Mon, 8 Jan 2024 08:06:53 +0200 Subject: [PATCH 7/9] guzzle http --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7e4d9a91..00a1dc20 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "require": { "php": ">=8.1", - "guzzle/http": "^3.9", + "guzzlehttp/guzzle": "^7.8", "illuminate/notifications": "^10.0", "illuminate/support": "^10.0" }, From e5bee9ae68c8d081033c30711d090e754ad4f19b Mon Sep 17 00:00:00 2001 From: cengiz Date: Mon, 8 Jan 2024 11:35:05 +0200 Subject: [PATCH 8/9] style fix --- src/Ntfy.php | 24 +++++++++++------------- src/NtfyChannel.php | 6 +++--- src/NtfyMessage.php | 11 +++++------ src/NtfyServiceProvider.php | 2 +- tests/NtfyChannelTest.php | 3 +-- tests/NtfyMessageTest.php | 2 -- 6 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/Ntfy.php b/src/Ntfy.php index a1f0b958..b3322695 100644 --- a/src/Ntfy.php +++ b/src/Ntfy.php @@ -2,8 +2,6 @@ namespace NotificationChannels\Ntfy; - -use GuzzleHttp\Psr7\Request; use NotificationChannels\Ntfy\Exceptions\CouldNotSendNotification; class Ntfy @@ -27,28 +25,28 @@ public function __construct($host, $port, $username, $password) public function send(NtfyMessage $message) { try { - $response = $this->client->post($this->host . ':' . $this->port, [ + $response = $this->client->post($this->host.':'.$this->port, [ 'auth' => [$this->username, $this->password], 'headers' => [ 'Content-Type' => 'application/json', 'Accept' => 'application/json', ], 'json' => [ - "topic" => $message->topic, - "message" => $message->content, - "title" => $message->title, - "priority" => $message->priority, - "tags" => [], - "attach" => "", - "filename" => "", - "click" => "", - "actions" => [], + 'topic' => $message->topic, + 'message' => $message->content, + 'title' => $message->title, + 'priority' => $message->priority, + 'tags' => [], + 'attach' => '', + 'filename' => '', + 'click' => '', + 'actions' => [], ], ]); } catch (\Exception $e) { throw new CouldNotSendNotification($e); } + return $response; } - } diff --git a/src/NtfyChannel.php b/src/NtfyChannel.php index 33a0163c..f7ad6470 100644 --- a/src/NtfyChannel.php +++ b/src/NtfyChannel.php @@ -2,8 +2,8 @@ namespace NotificationChannels\Ntfy; -use NotificationChannels\Ntfy\Exceptions\CouldNotSendNotification; use Illuminate\Notifications\Notification; +use NotificationChannels\Ntfy\Exceptions\CouldNotSendNotification; class NtfyChannel { @@ -18,14 +18,14 @@ public function __construct(Ntfy $service) * Send the given notification. * * @param mixed $notifiable - * @param \Illuminate\Notifications\Notification $notification + * @param \Illuminate\Notifications\Notification $notification * * @throws \NotificationChannels\Ntfy\Exceptions\CouldNotSendNotification */ public function send($notifiable, Notification $notification) { //make sure that the $notification object has a toNtfy() method - if (!method_exists($notification, 'toNtfy')) { + if (! method_exists($notification, 'toNtfy')) { throw CouldNotSendNotification::missingNtfyMethod(); } $this->service->send($notification->toNtfy($notifiable)); diff --git a/src/NtfyMessage.php b/src/NtfyMessage.php index e9c175fc..14949d0a 100644 --- a/src/NtfyMessage.php +++ b/src/NtfyMessage.php @@ -2,8 +2,6 @@ namespace NotificationChannels\Ntfy; -use Illuminate\Support\Arr; - class NtfyMessage { public $title; @@ -18,18 +16,21 @@ class NtfyMessage public function title(string $title): NtfyMessage { $this->title = $title; + return $this; } public function content(string $content): NtfyMessage { $this->content = $content; + return $this; } public function priority(int $priority): NtfyMessage { $this->priority = $priority; + return $this; } @@ -41,16 +42,14 @@ public function body(string $body): NtfyMessage public function topic(string $topic): NtfyMessage { $this->topic = $topic; + return $this; } public function actions($actions): NtfyMessage { $this->actions = $actions; + return $this; } - - - - } diff --git a/src/NtfyServiceProvider.php b/src/NtfyServiceProvider.php index eda9db2f..e94370fb 100644 --- a/src/NtfyServiceProvider.php +++ b/src/NtfyServiceProvider.php @@ -15,6 +15,7 @@ public function boot() ->needs(Ntfy::class) ->give(function () { $ntfyConfig = config('broadcasting.connections.ntfy'); + return new Ntfy( $ntfyConfig['host'], $ntfyConfig['port'], @@ -24,5 +25,4 @@ public function boot() ); }); } - } diff --git a/tests/NtfyChannelTest.php b/tests/NtfyChannelTest.php index 1088ecd8..cc846f15 100644 --- a/tests/NtfyChannelTest.php +++ b/tests/NtfyChannelTest.php @@ -7,14 +7,13 @@ class NtfyChannelTest extends TestCase { - public function test_send_should_throw_method_missing_exception() { $this->expectException(\NotificationChannels\Ntfy\Exceptions\CouldNotSendNotification::class); + $client = $this->getMockBuilder(\Psr\Http\Client\ClientInterface::class)->getMock(); $channel = new NtfyChannel(new \NotificationChannels\Ntfy\Ntfy('host', 'port', 'username', 'password', $client)); $notification = $this->getMockBuilder(\Illuminate\Notifications\Notification::class)->getMock(); $channel->send(new \stdClass(), $notification); } - } diff --git a/tests/NtfyMessageTest.php b/tests/NtfyMessageTest.php index 92939f26..7d6544dc 100644 --- a/tests/NtfyMessageTest.php +++ b/tests/NtfyMessageTest.php @@ -1,12 +1,10 @@ Date: Mon, 8 Jan 2024 11:38:35 +0200 Subject: [PATCH 9/9] style fix --- src/NtfyChannel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NtfyChannel.php b/src/NtfyChannel.php index f7ad6470..c45622f3 100644 --- a/src/NtfyChannel.php +++ b/src/NtfyChannel.php @@ -17,7 +17,7 @@ public function __construct(Ntfy $service) /** * Send the given notification. * - * @param mixed $notifiable + * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * * @throws \NotificationChannels\Ntfy\Exceptions\CouldNotSendNotification