public function notify(User $user, Event $event)
 {
     if ($user->getPhone()) {
         /* @var $template PushNotificationTemplate */
         //we know it's a PushNotificationTemplate, because we asked for it by type
         $template = $this->getTemplate($user, $event, new PushNotificationTemplate());
         echo "Sending push notification to " . $user->getPhone() . ":\n";
         echo $template->getText() . "\n\n";
         //we would probably have an API for sending push notifications which we would call here...
     }
 }
 public function notify(User $user, Event $event)
 {
     if ($user->getEmail()) {
         /* @var $template MailTemplate */
         //we know it's a MailTemplate, because we asked for it by type
         $template = $this->getTemplate($user, $event, new MailTemplate());
         /* In a real world application a more sophisticated
          * mailing mechanism would be used. For the purposes
          * of this exercise, I'm using the most primitive method.
          */
         echo "Sending mail to " . $user->getEmail() . ":\n";
         echo $template->getSubject() . "\n";
         echo $template->getText() . "\n\n";
         //mail($user->getEmail(), $template->getSubject(), $template->getBody());
     }
 }
 public function handleEvent(Event $event)
 {
     //not very efficient...if we used DB storage, we could filter by certain criteria
     $users = User::findAll();
     /* @var $user User */
     foreach ($users as $user) {
         if ($user->isSubscribedToEvent($event)) {
             $user->getNotificationStrategy()->notify($user, $event);
         }
     }
 }
/**
 * In a real-world system, the following entities would be created in CMS by an admin using CRUD
 */
$user1 = new User();
$user1->setName('Pavle Predic');
$user1->setEmail('*****@*****.**');
$user1->setRole(User::ROLE_MODERATOR);
$user1->setNotificationStrategy(new EmailNotificationStrategy());
$user1->subscribeToEvent(new NewCommentEvent());
$user2 = new User();
$user2->setName('Enzo Molinari');
$user2->setPhone('+3811132956');
$user2->setRole(User::ROLE_MODERATOR);
$user2->setNotificationStrategy(new SmsNotificationStrategy());
$user2->subscribeToEvent(new NewCommentEvent());
$user3 = new User();
$user3->setName('Administrator');
$user3->setEmail('*****@*****.**');
$user3->setRole(User::ROLE_ADMIN);
$user3->setNotificationStrategy(new EmailNotificationStrategy());
$user3->subscribeToEvent(new NewArticleEvent());
$newCommentMailTemplate = new MailTemplate();
$newCommentMailTemplate->setSubject('New comment has been posted');
$newCommentMailTemplate->setText('Dear %user%, new comment has been posted on your site on page "%page%". Go to comments panel and moderate it.');
$newCommentMailTemplate->setEvent(new NewCommentEvent());
$newCommentSmsTemplate = new SmsTemplate();
$newCommentSmsTemplate->setText('New comment has been posted on page "%page%".');
$newCommentSmsTemplate->setEvent(new NewCommentEvent());
$newArticleMailTemplate = new MailTemplate();
$newArticleMailTemplate->setSubject('New post has been created');
$newArticleMailTemplate->setText('Dear %user%, new article has been created on your site in category "%category%". Go to articles panel and moderate it.');