PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/EmailModule.php

https://github.com/whale2/users
PHP | 75 lines | 38 code | 13 blank | 24 comment | 10 complexity | 9e300439875e8446c2480fff31ef16e5 MD5 | raw file
  1. <?php
  2. interface IEmailModule extends IUserBaseModule
  3. {
  4. /**
  5. * This function should be called when new user is created
  6. * or email is recorded for the user for the first time
  7. */
  8. public function registerSubscriber($user);
  9. /**
  10. * This function should be called when user information has changed
  11. * e.g. email address or additional information passed to provider like name or gender and etc.
  12. */
  13. public function updateSubscriber($old_user, $new_user);
  14. /**
  15. * This function should be called when user chose to unsubscribe from the mailing list
  16. */
  17. public function removeSubscriber($user);
  18. /**
  19. * This method will be called if some user info is changed
  20. */
  21. public function userChanged($old_user, $new_user);
  22. /**
  23. * This method should be called by userChanged to decide if updateSubscriber needs to be called
  24. * It's up to implementing class to decide if email provider needs to be updated
  25. *
  26. * @return boolean Returns true if user's information has changed and needs to be synced
  27. */
  28. public function hasUserInfoChanged($old_user, $new_user);
  29. }
  30. abstract class EmailModule extends UserBaseModule implements IEmailModule {
  31. public function __construct() {
  32. parent::__construct();
  33. if (!is_null(UserConfig::$email_module)) {
  34. throw new EmailModuleException("You can assign only one email module");
  35. }
  36. UserConfig::$email_module = $this;
  37. }
  38. public function userChanged($old_user, $new_user) {
  39. // submodule to decide if it needs to sync the user info
  40. $userInfoChanged = $this->hasUserInfoChanged($old_user, $new_user);
  41. if (($old_user->getEmail() != $new_user->getEmail()) || $userInfoChanged) {
  42. try {
  43. if (is_null($old_user->getEmail()) && !is_null($new_user->getEmail())) {
  44. // new subscriber - they just got an email address
  45. UserConfig::$email_module->registerSubscriber($new_user);
  46. } else if (!is_null($old_user->getEmail()) && is_null($new_user->getEmail())) {
  47. // delete subscriber - they no longer have email address with us
  48. UserConfig::$email_module->removeSubscriber($old_user);
  49. } else {
  50. // update subscriber info
  51. UserConfig::$email_module->updateSubscriber($old_user, $new_user);
  52. }
  53. } catch (EmailModuleException $e) {
  54. error_log($e."\n[User Info]: ".var_export($new_user, true));
  55. }
  56. }
  57. }
  58. }
  59. class EmailModuleException extends Exception {}
  60. class EmailSubscriptionException extends EmailModuleException { }
  61. class EmailSubscriberUpdateException extends EmailModuleException { }
  62. class EmailUnSubscriptionException extends EmailModuleException { }