PHP code example of polaris / messaging

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

    

polaris / messaging example snippets


use Polaris\Audit\AuditPlugin;
use Polaris\Messaging\Channel\SymfonyMailerChannel;
use Polaris\Messaging\Channel\TwilioSmsChannel;
use Polaris\Messaging\MessagingPlugin;

$polaris = Polaris::create(new Config(
    ...,                                               // leave `mailer` and `sms` unset: the plugin provides them
    plugins: [new AuditPlugin(), new MessagingPlugin(
        channels: [new SymfonyMailerChannel($mailer, '[email protected]', $clock), new TwilioSmsChannel($psr18, $psr17, $psr17, $clock, $sid, $token, $from)],
        locale: 'en',
        templates: ['en' => ['email.verify' => ['subject' => 'Welcome to Acme', 'text' => 'Verify at https://acme.example/verify/{token}']]],
        caps: ['*' => [5, 600], 'sms.otp' => [3, 600]],   // per recipient and template: [limit, window seconds]
    )],
));

$graph->get(Sender::class)->send('email', $user->email, 'email.new_device', ['ip' => $ip, 'user_agent' => $ua], locale: 'es', organizationId: $orgId);

use Polaris\Messaging\{Message, MessagingPlugin, Outbox, Receipt, Sender};

final class QueueOutbox implements Outbox
{
    public function __construct(private Queue $queue) {}

    public function push(Message $message, array $alternatives): ?Receipt
    {
        $this->queue->publish(serialize([$message, $alternatives]));

        return Receipt::queued(new DateTimeImmutable());
    }
}

new MessagingPlugin(channels: [$mail, $sms], outbox: new QueueOutbox($queue));

// the worker, on the same graph:
[$message, $alternatives] = unserialize($job, ['allowed_classes' => [Message::class]]);
$polaris->graph()->get(Sender::class)->dispatch($message, $alternatives);