Skip to content

Latest commit

 

History

History
60 lines (49 loc) · 1.61 KB

File metadata and controls

60 lines (49 loc) · 1.61 KB

Custom Driver

Provider packages should implement NotificationDriverInterface. Extending AbstractNotificationDriver keeps custom drivers compact.

<?php

declare(strict_types=1);

use CommonPHP\Notifications\Contracts\AbstractNotificationDriver;
use CommonPHP\Notifications\DeliveryResult;
use CommonPHP\Notifications\Notification;

final class AcmeEmailDriver extends AbstractNotificationDriver
{
    public function __construct(private AcmeEmailClient $client)
    {
    }

    public function supports(Notification $notification, string $channel): bool
    {
        return $channel === 'email';
    }

    public function send(Notification $notification, string $channel): DeliveryResult
    {
        $response = $this->client->send([
            'subject' => $notification->subject(),
            'body' => $notification->body(),
            'to' => array_map(
                static fn ($recipient): string => $recipient->address(),
                $notification->recipientsForChannel($channel),
            ),
            'metadata' => $notification->metadataItems(),
        ]);

        if ($response->queued) {
            return $this->queued(
                $notification,
                $channel,
                providerMessageId: $response->id,
                details: ['provider' => 'acme'],
            );
        }

        return $this->sent(
            $notification,
            $channel,
            providerMessageId: $response->id,
            details: ['provider' => 'acme'],
        );
    }
}

Register the driver:

$notifier->useChannel('email', new AcmeEmailDriver($client), default: true);