Skip to content

Commit

Permalink
Factor out email sending into its own service
Browse files Browse the repository at this point in the history
- for later reuse.
  • Loading branch information
gregcorbett committed Aug 26, 2021
1 parent 677ba4f commit 7a55ff8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
47 changes: 47 additions & 0 deletions lib/Gocdb_Services/EmailService.php
@@ -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;
}
}
14 changes: 14 additions & 0 deletions lib/Gocdb_Services/Factory.php
Expand Up @@ -48,6 +48,7 @@ class Factory {
private static $OwnedEntityService = null;
private static $exService = null;
private static $notificationService = null;
private static $emailService = null;

public static $properties = array();
//private static $properties = null;
Expand Down Expand Up @@ -385,6 +386,19 @@ public static function getNotificationService() {
}
return self::$notificationService;
}

/**
* Singleton EmailService
* @return org\gocdb\services\EmailService
*/
public static function getEmailService() {
if (self::$emailService == null) {
require_once __DIR__ . '/EmailService.php';
self::$emailService = new org\gocdb\services\EmailService();
self::$emailService->setEntityManager(self::getEntityManager());
}
return self::$emailService;
}
}

?>
30 changes: 2 additions & 28 deletions lib/Gocdb_Services/NotificationService.php
Expand Up @@ -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',
Expand All @@ -152,13 +130,9 @@ private function sendEmail($roleRequested, $requestingUser, $entityName, $approv
$this->getWebPortalURL()
);

$email = $approving_user->getEmail();
$emailAddress = $approvingUser->getEmail();
$headers = "From: GOCDB <gocdb-admins@mailman.egi.eu>";

if ($this->getConfigSendEmail()) {
$this->mail($email, $subject, $body, $headers);
} else {
$this->mockMail($email, $subject, $body, $headers);
}
\Factory::getEmailService()->send($emailAddress, $subject, $body, $headers);
}
}

0 comments on commit 7a55ff8

Please sign in to comment.