Last active
August 23, 2019 14:22
-
-
Save n1215/ac3a9215a40a21b485f38ac00113f173 to your computer and use it in GitHub Desktop.
Event based notifications library idea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$container = new SomePSR11Container(); | |
// $container->regiseter(); | |
$notifications = new N1215\Notification\Registrar($container); | |
$notifications | |
->source(N1215\Events\UserRegistered::class) | |
->to(N1215\Notification\UserTargetSelector::class) | |
->inSingleFormOf( | |
N1215\Notification\UserRegisteredMailTemplate::class, | |
N1215\Notification\UserRegisteredSmsTemplate::class | |
) | |
->retry(3); | |
$notifications | |
->source(N1215\Events\UserRegistered::class) | |
->to(N1215\Notification\SystemTargetSelector::class) | |
->inMultipleFormsOf( | |
N1215\Notification\UserRegisteredMailTemplateForSystem::class, | |
N1215\Notification\UserRegisteredSlackTemplateForSystem::class | |
) | |
->retry(2); | |
$notifications | |
->source(N1215\Events\UserRegistered::class) | |
->to(N1215\Notification\LogTargetSelector::class) | |
->inMultipleFormsOf( | |
N1215\Notification\UserRegisteredLogTemplate::class, | |
) | |
->retry(2); | |
$notifictionCenter = $notifications->build(); | |
// implements PSR-14's Listener Provider Interface | |
$userRegistered = new UserRegistered(new User('[email protected]'); | |
$listeners = $notifictionCenter->getListenersForEvent($userRegistered); | |
// or implements PSR-14's EventDispatcher Interface | |
$notificationCenter->dispatch($userRegistered); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace N1215\Models; | |
class User implements NotificationTargetInterface | |
{ | |
private string $email; | |
public function __construct(string $email) | |
{ | |
$this->email = $email; | |
} | |
public function getNotificationSinks(): NotificationSinkInterface[] | |
{ | |
return [new MailSink($this->email)]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace N1215\Events; | |
class UserRegistered | |
{ | |
private User $user; | |
private DateTime $registeredAt; | |
public function __cosntruct(User $user, DateTime $registeredAt) | |
{ | |
$this->user = $user; | |
$this->registeredAt = $registeredAt; | |
} | |
public function getUser(): User | |
{ | |
return $user; | |
} | |
public function getRegisteredAt(): DateTime | |
{ | |
return $this->registeredAt; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace N1215\Notification; | |
class MailChannel implements ChannelIntetface | |
{ | |
/** | |
* @param MessageInterface $message | |
* @param SinkInterface $sink | |
* | |
*/ | |
public function send(MessageInterface $message, SinkInterface $sink) | |
{ | |
// send mail | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace N1215\Notification; | |
class UserTargetSelector | |
{ | |
public function supportsEvent($event): bool | |
{ | |
return $event instanceof UserRegistered; | |
} | |
public function select($event): NotificationTargetInterface | |
{ | |
return $event->getUser(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace N1215\Notification; | |
class UserRegisteredTemplate implements MessageTemplate { | |
public function supportsEvent($event): bool | |
{ | |
return $event instanceof UserRegistered; | |
} | |
public function getChannel(): string | |
{ | |
return MailChannel::class; | |
} | |
public function render($event, SinkInterface $sink): MailMessage | |
{ | |
return new MailMessage(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment