PHP code example of waaseyaa / notification

1. Go to this page and download the library: Download waaseyaa/notification 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/ */

    

waaseyaa / notification example snippets


interface NotificationInterface
{
    /** @return list<string> channel names, e.g. ['mail', 'database'] */
    public function via(NotifiableInterface $notifiable): array;
    /** @return array<string, mixed> */
    public function toArray(NotifiableInterface $notifiable): array;
    // Optional, resolved by channels via method_exists():
    //   toMail(NotifiableInterface): \Waaseyaa\Mail\Envelope
    //   toDatabase(NotifiableInterface): array<string, mixed>
}

interface NotifiableInterface          // @api
{
    public function routeNotificationFor(string $channel): mixed; // 'mail' => email, 'database' => id
    public function getNotifiableId(): string;
    public function getNotifiableType(): string;
}

interface ChannelInterface
{
    public function send(NotifiableInterface $notifiable, NotificationInterface $notification): void;
}

final class NotificationDispatcher     // @api
{
    /** @param array<string, ChannelInterface> $channels */
    public function __construct(QueueInterface $queue, array $channels, ?LoggerInterface $logger = null);
    public function send(NotifiableInterface $notifiable, NotificationInterface $notification): void;       // sync
    public function sendAsync(NotifiableInterface $notifiable, NotificationInterface $notification): void;  // queued
    /** @param iterable<NotifiableInterface> $notifiables */
    public function sendToMany(iterable $notifiables, NotificationInterface $notification): void;
    /** @return array<string, ChannelInterface> */
    public function channels(): array;
}

use Waaseyaa\Notification\{NotifiableInterface, NotificationInterface, NotificationDispatcher};
use Waaseyaa\Notification\Channel\MailChannel;
use Waaseyaa\Mail\{Envelope, Mailer};
use Waaseyaa\Queue\SyncQueue;

$notification = new class implements NotificationInterface {
    public function via(NotifiableInterface $n): array { return ['mail']; }
    public function toArray(NotifiableInterface $n): array { return ['message' => 'Welcome']; }
    public function toMail(NotifiableInterface $n): Envelope {
        return new Envelope(
            to: [$n->routeNotificationFor('mail')],
            from: '[email protected]',
            subject: 'Welcome',
            textBody: 'Thanks for joining.',
        );
    }
};

$dispatcher = new NotificationDispatcher(
    new SyncQueue(),
    ['mail' => new MailChannel($mailer)],
);

$dispatcher->send($user, $notification);   // $user implements NotifiableInterface