Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php declare(strict_types = 1);
- class MessageService
- {
- protected SenderStrategy $senderStrategy;
- public function __construct(SenderStrategy $senderStrategy)
- {
- $this->senderStrategy = $senderStrategy;
- }
- public function send(string $message)
- {
- $this->senderStrategy->send($message);
- }
- }
- interface SenderStrategy
- {
- public function send(string $message);
- }
- class EmailChannel implements SenderStrategy
- {
- public function send(string $message)
- {
- $success = file_put_contents('email', $message);
- if ($success) print 'Email Message sent!'."\n";
- }
- }
- class SMSChannel implements SenderStrategy
- {
- public function send(string $message)
- {
- $success = file_put_contents('sms', $message);
- if ($success) print 'SMS Message sent!'."\n";
- }
- }
- class PushNotificationChannel implements SenderStrategy
- {
- public function send(string $message)
- {
- $success = file_put_contents('push_notification', $message);
- if ($success) print 'Push notification Message sent!'."\n";
- }
- }
- class CartaChannel implements SenderStrategy
- {
- public function send(string $message)
- {
- $success = file_put_contents('carta', $message);
- if ($success) print 'Carta Message sent!'."\n";
- }
- }
- $user = (object) [
- 'subscription_channels' => [
- SMSChannel::class,
- EmailChannel::class,
- PushNotificationChannel::class,
- CartaChannel::class,
- ] ,
- ];
- $messagemDoSite = 'Olhe essa promoção'."\n";
- foreach ($user->subscription_channels as $channel)
- {
- $messageService = new MessageService(
- new $channel(),
- );
- $messageService->send($messagemDoSite);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement