PHP code example of snb4crazy / notifyhub-laravel

1. Go to this page and download the library: Download snb4crazy/notifyhub-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

snb4crazy / notifyhub-laravel example snippets


use NotifyHub\LaravelClient\Facades\NotifyHub;
use NotifyHub\LaravelClient\Data\EventPayload;

NotifyHub::send(new EventPayload(
    title: 'Payment declined',
    message: 'Stripe returned error code card_declined',
    severity: 'error',
    eventType: 'payment.failed',
    application: config('app.name'),
    environment: app()->environment(),
    context: ['order_id' => $order->id],
));

try {
    $this->chargeCard($order);
} catch (\Throwable $e) {
    NotifyHub::sendException($e, ['order_id' => $order->id]);
    throw $e;
}

NotifyHub::send(EventPayload::fromException($exception));
NotifyHub::send(EventPayload::fromFailedJob(SendEmail::class, $exception));
NotifyHub::send(EventPayload::fromFailedCron('nightly-sync', 'exit 1'));

use NotifyHub\LaravelClient\Contracts\NotifyHubClientInterface;

class MyService
{
    public function __construct(private NotifyHubClientInterface $notifyHub) {}

    public function doWork(): void
    {
        try {
            // ...
        } catch (\Throwable $e) {
            $this->notifyHub->sendException($e);
        }
    }
}

NotifyHub::sendRaw([
    'title'    => 'Custom alert',
    'message'  => 'Something interesting happened',
    'severity' => 'info',
    'event_type' => 'custom.event',
]);

// bootstrap/app.php
->withExceptions(function (Exceptions $exceptions) {
    $exceptions->report(function (\Throwable $e) {
        rescue(fn () => app(\NotifyHub\LaravelClient\Contracts\NotifyHubClientInterface::class)
            ->sendException($e));
    });
})

public function register(): void
{
    $this->reportable(function (\Throwable $e) {
        rescue(fn () => app(\NotifyHub\LaravelClient\Contracts\NotifyHubClientInterface::class)
            ->sendException($e));
    });
}
bash
php artisan vendor:publish --provider="NotifyHub\\LaravelClient\\NotifyHubServiceProvider" --tag=notifyhub-config