Skip to content

Commit

Permalink
refactor with php-notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Feb 11, 2024
1 parent 3ae58ff commit a559a0a
Show file tree
Hide file tree
Showing 32 changed files with 268 additions and 1,544 deletions.
80 changes: 80 additions & 0 deletions .github/workflows/run-coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Coverage

on:
push:
paths:
- "**.php"
- ".github/workflows/run-tests-pest.yml"
- "phpunit.xml.dist"
- "composer.json"
- "composer.lock"

jobs:
test:
runs-on: ${{ matrix.os }}
timeout-minutes: 5
strategy:
max-parallel: 1
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.1]
stability: [prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: pcov

- name: Setup mailpit
run: |
docker run -d \
--restart unless-stopped \
--name=mailpit \
-p 8025:8025 \
-p 1025:1025 \
axllent/mailpit
- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction

- name: List Installed Dependencies
run: composer show -D

- name: Create .env
run: |
touch .env
echo NOTIFIER_DISCORD_WEBHOOK=${{ secrets.NOTIFIER_DISCORD_WEBHOOK }} >> .env
echo NOTIFIER_SLACK_WEBHOOK=${{ secrets.NOTIFIER_SLACK_WEBHOOK }} >> .env
echo NOTIFIER_MAIL_MAILER=${{ secrets.NOTIFIER_MAIL_MAILER }} >> .env
echo NOTIFIER_MAIL_HOST=${{ secrets.NOTIFIER_MAIL_HOST }} >> .env
echo NOTIFIER_MAIL_PORT=${{ secrets.NOTIFIER_MAIL_PORT }} >> .env
echo NOTIFIER_MAIL_USERNAME=${{ secrets.NOTIFIER_MAIL_USERNAME }} >> .env
echo NOTIFIER_MAIL_PASSWORD=${{ secrets.NOTIFIER_MAIL_PASSWORD }} >> .env
echo NOTIFIER_MAIL_ENCRYPTION=${{ secrets.NOTIFIER_MAIL_ENCRYPTION }} >> .env
echo NOTIFIER_MAIL_FROM_ADDRESS=${{ secrets.NOTIFIER_MAIL_FROM_ADDRESS }} >> .env
echo NOTIFIER_MAIL_TO_ADDRESS=${{ secrets.NOTIFIER_MAIL_TO_ADDRESS }} >> .env
echo NOTIFIER_REQUEST=${{ secrets.NOTIFIER_REQUEST }} >> .env
echo NOTIFIER_MOCK=false >> .env
- name: Execute tests
run: vendor/bin/pest --coverage

- name: Send code coverage
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
verbose: false
8 changes: 1 addition & 7 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,4 @@ jobs:
echo NOTIFIER_MAIL_TO_NAME=${{ secrets.NOTIFIER_MAIL_TO_NAME }} >> .env
- name: Execute tests
run: vendor/bin/pest --coverage

- name: Send code coverage
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
verbose: false
run: vendor/bin/pest --ci
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
Notifier for Laravel is a package to add some useful classes to send notifications (with `Notifier::class`) and monitoring (with `Journal::class`). The configuration file is totally optional, if you have multiple webhooks, you can create your own configs to send notifications.

Based on [`kiwilan/php-notifier`](https://github.com/kiwilan/php-notifier).

Works for [Discord webhooks](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks), [Slack webhooks](https://api.slack.com/messaging/webhooks) and emails with [`symfony/mailer`](https://symfony.com/doc/current/mailer.html).

## About
Expand Down Expand Up @@ -84,6 +86,11 @@ return [
'webhook' => env('NOTIFIER_SLACK_WEBHOOK', null),
],

'http' => [
// Default HTTP URL to send request.
'url' => env('NOTIFIER_HTTP_URL', null),
],

// This feature use `filament/notifications` package, not included in this package.
'to_database' => [
// Default user model for notification.
Expand Down Expand Up @@ -170,7 +177,7 @@ class Handler extends ExceptionHandler
Notifier is an alternative to [Laravel Notifications](https://laravel.com/docs/10.x/notifications).

> [!NOTE]
> If `app.debug` is `true`, `debug` level logs will be written for sending and sent notifications.
> If `notifier.journal.debug` is `true`, `debug` level logs will be written for sending and sent notifications.
> In all cases, `error` level logs will be written for sending errors.
#### Discord
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"require": {
"php": "^8.1",
"illuminate/contracts": "^10.0",
"spatie/laravel-package-tools": "^1.14.0",
"symfony/mailer": "^6.4"
"kiwilan/php-notifier": "^0.0.31",
"spatie/laravel-package-tools": "^1.14.0"
},
"require-dev": {
"filament/notifications": "^3.2",
Expand All @@ -42,13 +42,13 @@
},
"autoload": {
"psr-4": {
"Kiwilan\\Notifier\\": "src/",
"Kiwilan\\Notifier\\Database\\Factories\\": "database/factories/"
"Kiwilan\\LaravelNotifier\\": "src/",
"Kiwilan\\LaravelNotifier\\Database\\Factories\\": "database/factories/"
}
},
"autoload-dev": {
"psr-4": {
"Kiwilan\\Notifier\\Tests\\": "tests/",
"Kiwilan\\LaravelNotifier\\Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/"
}
},
Expand Down Expand Up @@ -81,10 +81,10 @@
"extra": {
"laravel": {
"providers": [
"Kiwilan\\Notifier\\NotifierServiceProvider"
"Kiwilan\\LaravelNotifier\\LaravelNotifierServiceProvider"
],
"aliases": {
"Notifier": "Kiwilan\\Notifier\\Facades\\Notifier"
"LaravelNotifier": "Kiwilan\\LaravelNotifier\\Facades\\Notifier"
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions config/notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
'webhook' => env('NOTIFIER_SLACK_WEBHOOK', null),
],

'http' => [
// Default HTTP URL to send request.
'url' => env('NOTIFIER_HTTP_URL', null),
],

// This feature use `filament/notifications` package, not included in this package.
'to_database' => [
// Default user model for notification.
Expand All @@ -46,6 +51,7 @@
],

'journal' => [
'debug' => env('NOTIFIER_JOURNAL_DEBUG', false),
// Write error logs with `error_log` function, in addition to Laravel log.
'use_error_log' => env('NOTIFIER_JOURNAL_USE_ERROR_LOG', true),
],
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/NotifierCommand.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Kiwilan\Notifier\Commands;
namespace Kiwilan\LaravelNotifier\Commands;

use Illuminate\Console\Command;
use Kiwilan\Notifier\Facades\Notifier;
use Kiwilan\LaravelNotifier\Facades\Notifier;

class NotifierCommand extends Command
{
Expand Down
16 changes: 8 additions & 8 deletions src/Facades/Journal.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?php

namespace Kiwilan\Notifier\Facades;
namespace Kiwilan\LaravelNotifier\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @see \Kiwilan\Notifier\Journal
* @see \Kiwilan\LaravelNotifier\Journal
*
* @method static \Kiwilan\Notifier\Journal info(string $message, array|string $data = [])
* @method static \Kiwilan\Notifier\Journal debug(string $message, array|string $data = [])
* @method static \Kiwilan\Notifier\Journal warning(string $message, array|string $data = [])
* @method static \Kiwilan\Notifier\Journal error(string $message, array|string $data = [])
* @method static \Kiwilan\Notifier\Journal handler(\Throwable $e, bool $toDatabase = false, ?string $toNotifier = null)
* @method static \Kiwilan\LaravelNotifier\Journal info(string $message, array|string $data = [])
* @method static \Kiwilan\LaravelNotifier\Journal debug(string $message, array|string $data = [])
* @method static \Kiwilan\LaravelNotifier\Journal warning(string $message, array|string $data = [])
* @method static \Kiwilan\LaravelNotifier\Journal error(string $message, array|string $data = [])
* @method static \Kiwilan\LaravelNotifier\Journal handler(\Throwable $e, bool $toDatabase = false, ?string $toNotifier = null)
*/
class Journal extends Facade
{
protected static function getFacadeAccessor()
{
return \Kiwilan\Notifier\Journal::class;
return \Kiwilan\LaravelNotifier\Journal::class;
}
}
11 changes: 6 additions & 5 deletions src/Facades/Notifier.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?php

namespace Kiwilan\Notifier\Facades;
namespace Kiwilan\LaravelNotifier\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @see \Kiwilan\Notifier\Notifier
*
* @method static \Kiwilan\Notifier\Notifier\NotifierMail mail()
* @method static \Kiwilan\Notifier\Notifier\NotifierSlack slack(?string $webhook = null)
* @method static \Kiwilan\Notifier\Notifier\NotifierDiscord discord(?string $webhook = null)
* @method static \Kiwilan\Notifier\NotifierMail mail()
* @method static \Kiwilan\Notifier\NotifierSlack slack(?string $webhook = null, string $client = 'stream')
* @method static \Kiwilan\Notifier\NotifierDiscord discord(?string $webhook = null, string $client = 'stream')
* @method static \Kiwilan\Notifier\NotifierHttp http(?string $url = null, string $client = 'stream')
*/
class Notifier extends Facade
{
protected static function getFacadeAccessor()
{
return \Kiwilan\Notifier\Notifier::class;
return \Kiwilan\LaravelNotifier\Notifier::class;
}
}
6 changes: 5 additions & 1 deletion src/Journal.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

namespace Kiwilan\Notifier;
namespace Kiwilan\LaravelNotifier;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Kiwilan\LaravelNotifier\Facades\Notifier;

class Journal
{
Expand Down Expand Up @@ -93,6 +94,8 @@ private function log(): void
$data = json_encode($this->data, JSON_PRETTY_PRINT);
error_log("Journal: {$this->level} - {$this->message} - {$data}");
}

// dump($this->message, $this->data);
}

/**
Expand Down Expand Up @@ -149,6 +152,7 @@ public function toNotifier(string $type): self
'mail' => Notifier::mail(),
'slack' => Notifier::slack(),
'discord' => Notifier::discord(),
'http' => Notifier::http(),
default => null,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Kiwilan\Notifier;
namespace Kiwilan\LaravelNotifier;

use Kiwilan\Notifier\Commands\NotifierCommand;
use Kiwilan\LaravelNotifier\Commands\NotifierCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class NotifierServiceProvider extends PackageServiceProvider
class LaravelNotifierServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
Expand Down
Loading

0 comments on commit a559a0a

Please sign in to comment.