Skip to content

Instantly share code, notes, and snippets.

@mikemix
Last active August 26, 2022 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikemix/d7dc0a9c2c3bcdab6b7538d34c5c3c0e to your computer and use it in GitHub Desktop.
Save mikemix/d7dc0a9c2c3bcdab6b7538d34c5c3c0e to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace App\User\Notification;
use App\Notification\EmailNotificationInterface;
use App\Notification\NotificationManagerInterface;
use App\Events\PostRegistrationEvent;
use App\User\Notification\WelcomeGreetingEmailNotificationFactory;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class EmailNotification implements EventSubscriberInterface
{
private NotificationManagerInterface $notificationManager;
private WelcomeGreetingEmailNotificationFactory $notificationFactory;
public function __construct(
NotificationManagerInterface $notificationManager,
WelcomeGreetingEmailNotificationFactory $notificationFactory
) {
$this->notificationManager = $notificationManager;
$this->notificationFactory = $notificationFactory;
}
/** {@inheritDoc} */
public static function getSubscribedEvents(): array
{
return [PostRegistrationEvent::NAME => 'sendNotification'];
}
public function sendNotification(PostRegistrationEvent $event): void
{
$notification = ($this->notificationFactory($event->getUser());
$this->notificationManager->send([$notification]);
}
}
@Myks92
Copy link

Myks92 commented Aug 25, 2022

Fix:

public function sendNotification(PostRegistrationEvent $event): void
    {
        $notification = ($this->notificationFactory)($event->getUser());
        $this->notificationManager->send(new \ArrayObject($notification));
    }

@mikemix
Copy link
Author

mikemix commented Aug 26, 2022

Thx, I'll go for an array though. Should have created two methods though, sendOne and sendMany ;)

@Myks92
Copy link

Myks92 commented Aug 26, 2022

I think you should just choose send One) One notification is sent more often. And a few can be done through foreach)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment