-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out email sending into its own service
- for later reuse.
- Loading branch information
1 parent
677ba4f
commit 7a55ff8
Showing
3 changed files
with
63 additions
and
28 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace org\gocdb\services; | ||
|
||
require_once __DIR__ . '/AbstractEntityService.php'; | ||
require_once __DIR__ . '/Factory.php'; | ||
|
||
/** | ||
*/ | ||
class EmailService extends AbstractEntityService { | ||
/** | ||
* Depending on the configuration, either send an email or print what would | ||
* have been sent. | ||
* @param string $emailAddress A single email address to send to | ||
* @param string $subject The subject of the email | ||
* @param string $body The body of the email | ||
* @param string $headers The headers of the email | ||
*/ | ||
public function send($emailAddress, $subject, $body, $headers) { | ||
if ($this->getConfigSendEmail()) { | ||
$this->mail($emailAddress, $subject, $body, $headers); | ||
} else { | ||
$this->mockMail($emailAddress, $subject, $body, $headers); | ||
} | ||
} | ||
|
||
/** | ||
* Return whether send_email is enabled in the config file | ||
*/ | ||
private function getConfigSendEmail() { | ||
return \Factory::getConfigService()->getSendEmails(); | ||
} | ||
|
||
private function mockMail($to, $subject, $message, $additionalHeaders = "", $additionalParameters = "") { | ||
echo "<!--\n"; | ||
echo "Sending mail disabled, but would have sent:\n"; | ||
echo "$additionalHeaders\n"; | ||
echo "To: $to\n"; | ||
echo "Subject: $subject\n"; | ||
echo "\n$message\n"; | ||
echo "\nAdditional Parameters: $additionalParameters\n"; | ||
echo "-->\n"; | ||
return True; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,28 +106,6 @@ private function getWebPortalURL() { | |
return \Factory::getConfigService()->GetPortalURL(); | ||
} | ||
|
||
|
||
/** | ||
* Return whether send_email is enabled in the config file | ||
*/ | ||
private function getConfigSendEmail() { | ||
return \Factory::getConfigService()->getSendEmails(); | ||
} | ||
|
||
|
||
private function mockMail($to, $subject, $message, $additionalHeaders = "", $additionalParameters = "") { | ||
echo "<!--\n"; | ||
echo "Sending mail disabled, but would have sent:\n"; | ||
echo "$additionalHeaders\n"; | ||
echo "To: $to\n"; | ||
echo "Subject: $subject\n"; | ||
echo "\n$message\n"; | ||
echo "\nAdditional Parameters: $additionalParameters\n"; | ||
echo "-->\n"; | ||
return True; | ||
} | ||
|
||
|
||
private function sendEmail($roleRequested, $requestingUser, $entityName, $approvingUser) { | ||
$subject = sprintf( | ||
'GocDB: A Role request from %1$s over %2$s requires your attention', | ||
|
@@ -152,13 +130,9 @@ private function sendEmail($roleRequested, $requestingUser, $entityName, $approv | |
$this->getWebPortalURL() | ||
); | ||
|
||
$email = $approving_user->getEmail(); | ||
$emailAddress = $approvingUser->getEmail(); | ||
$headers = "From: GOCDB <[email protected]>"; | ||
|
||
if ($this->getConfigSendEmail()) { | ||
$this->mail($email, $subject, $body, $headers); | ||
} else { | ||
$this->mockMail($email, $subject, $body, $headers); | ||
} | ||
\Factory::getEmailService()->send($emailAddress, $subject, $body, $headers); | ||
} | ||
} |