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