Some refactoring to the email stuff
This commit is contained in:
parent
36f8de957a
commit
9832ca58ba
@ -9,7 +9,7 @@ use PHPMailer;
|
|||||||
|
|
||||||
interface EmailSender {
|
interface EmailSender {
|
||||||
/**
|
/**
|
||||||
* Use to send emails that do NOT include ticket information
|
* Use to send emails
|
||||||
*
|
*
|
||||||
* @param $emailBuilder EmailBuilder
|
* @param $emailBuilder EmailBuilder
|
||||||
* @param $heskSettings array
|
* @param $heskSettings array
|
||||||
|
35
api/BusinessLogic/Emails/EmailSenderHelper.php
Normal file
35
api/BusinessLogic/Emails/EmailSenderHelper.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BusinessLogic\Emails;
|
||||||
|
|
||||||
|
|
||||||
|
class EmailSenderHelper {
|
||||||
|
/**
|
||||||
|
* @var $emailTemplateParser EmailTemplateParser
|
||||||
|
*/
|
||||||
|
private $emailTemplateParser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var $basicEmailSender BasicEmailSender
|
||||||
|
*/
|
||||||
|
private $basicEmailSender;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var $mailgunEmailSender MailgunEmailSender
|
||||||
|
*/
|
||||||
|
private $mailgunEmailSender;
|
||||||
|
|
||||||
|
function __construct($emailTemplateParser, $basicEmailSender, $mailgunEmailSender) {
|
||||||
|
$this->emailTemplateParser = $emailTemplateParser;
|
||||||
|
$this->basicEmailSender = $basicEmailSender;
|
||||||
|
$this->mailgunEmailSender = $mailgunEmailSender;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendEmailForTicket($emailTemplateName, $ticket, $heskSettings, $modsForHeskSettings) {
|
||||||
|
//-- parse template
|
||||||
|
|
||||||
|
//-- if no mailgun, use basic sender
|
||||||
|
|
||||||
|
//-- otherwise use mailgun sender
|
||||||
|
}
|
||||||
|
}
|
27
api/BusinessLogic/Emails/EmailTemplate.php
Normal file
27
api/BusinessLogic/Emails/EmailTemplate.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BusinessLogic\Emails;
|
||||||
|
|
||||||
|
|
||||||
|
class EmailTemplate {
|
||||||
|
/**
|
||||||
|
* @var $languageKey string
|
||||||
|
*/
|
||||||
|
public $languageKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var $fileName string
|
||||||
|
*/
|
||||||
|
public $fileName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var $forStaff bool
|
||||||
|
*/
|
||||||
|
public $forStaff;
|
||||||
|
|
||||||
|
function __construct($forStaff, $fileName, $languageKey = null) {
|
||||||
|
$this->languageKey = $languageKey === null ? $fileName : $languageKey;
|
||||||
|
$this->fileName = $fileName;
|
||||||
|
$this->forStaff = $forStaff;
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: mkoch
|
|
||||||
* Date: 2/22/2017
|
|
||||||
* Time: 9:11 PM
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace BusinessLogic\Emails;
|
namespace BusinessLogic\Emails;
|
||||||
|
|
||||||
@ -35,27 +29,41 @@ class EmailTemplateParser {
|
|||||||
*/
|
*/
|
||||||
private $userGateway;
|
private $userGateway;
|
||||||
|
|
||||||
function __construct($statusGateway, $categoryGateway, $userGateway) {
|
/**
|
||||||
|
* @var $emailTemplateRetriever EmailTemplateRetriever
|
||||||
|
*/
|
||||||
|
private $emailTemplateRetriever;
|
||||||
|
|
||||||
|
function __construct($statusGateway, $categoryGateway, $userGateway, $emailTemplateRetriever) {
|
||||||
$this->statusGateway = $statusGateway;
|
$this->statusGateway = $statusGateway;
|
||||||
$this->categoryGateway = $categoryGateway;
|
$this->categoryGateway = $categoryGateway;
|
||||||
$this->userGateway = $userGateway;
|
$this->userGateway = $userGateway;
|
||||||
|
$this->emailTemplateRetriever = $emailTemplateRetriever;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $templateName string
|
* @param $templateId int
|
||||||
* @param $language string
|
* @param $language string
|
||||||
* @param $ticket Ticket
|
* @param $ticket Ticket
|
||||||
|
* @param $heskSettings array
|
||||||
|
* @param $modsForHeskSettings array
|
||||||
|
* @return ParsedEmailProperties
|
||||||
|
* @throws InvalidEmailTemplateException
|
||||||
*/
|
*/
|
||||||
function getFormattedEmailForLanguage($templateName, $language, $ticket, $forStaff, $heskSettings, $modsForHeskSettings) {
|
function getFormattedEmailForLanguage($templateId, $language, $ticket, $heskSettings, $modsForHeskSettings) {
|
||||||
global $hesklang;
|
$emailTemplate = $this->emailTemplateRetriever->getTemplate($templateId);
|
||||||
|
|
||||||
$template = self::getFromFileSystem($templateName, $language, false);
|
if ($emailTemplate === null) {
|
||||||
$htmlTemplate = self::getFromFileSystem($templateName, $language, true);
|
throw new InvalidEmailTemplateException($templateId);
|
||||||
$subject = ValidEmailTemplates::getValidEmailTemplates()[$templateName];
|
}
|
||||||
|
|
||||||
|
$template = self::getFromFileSystem($emailTemplate->fileName, $language, false);
|
||||||
|
$htmlTemplate = self::getFromFileSystem($emailTemplate->fileName, $language, true);
|
||||||
|
$subject = $emailTemplate->languageKey;
|
||||||
|
|
||||||
$subject = $this->parseSubject($subject, $ticket, $language, $heskSettings);
|
$subject = $this->parseSubject($subject, $ticket, $language, $heskSettings);
|
||||||
$message = $this->parseMessage($template, $ticket, $language, $forStaff, $heskSettings, $modsForHeskSettings, false);
|
$message = $this->parseMessage($template, $ticket, $language, $emailTemplate->forStaff, $heskSettings, $modsForHeskSettings, false);
|
||||||
$htmlMessage = $this->parseMessage($htmlTemplate, $ticket, $language, $forStaff, $heskSettings, $modsForHeskSettings, true);
|
$htmlMessage = $this->parseMessage($htmlTemplate, $ticket, $language, $emailTemplate->forStaff, $heskSettings, $modsForHeskSettings, true);
|
||||||
|
|
||||||
return new ParsedEmailProperties($subject, $message, $htmlMessage);
|
return new ParsedEmailProperties($subject, $message, $htmlMessage);
|
||||||
}
|
}
|
||||||
@ -66,13 +74,9 @@ class EmailTemplateParser {
|
|||||||
* @param $html bool
|
* @param $html bool
|
||||||
* @return string The template
|
* @return string The template
|
||||||
* @throws EmailTemplateNotFoundException If the template was not found in the filesystem for the provided language
|
* @throws EmailTemplateNotFoundException If the template was not found in the filesystem for the provided language
|
||||||
* @throws InvalidEmailTemplateException If the $template is not a valid template name
|
|
||||||
*/
|
*/
|
||||||
private function getFromFileSystem($template, $language, $html)
|
private function getFromFileSystem($template, $language, $html)
|
||||||
{
|
{
|
||||||
if (!isset(ValidEmailTemplates::getValidEmailTemplates()[$template])) {
|
|
||||||
throw new InvalidEmailTemplateException($template);
|
|
||||||
}
|
|
||||||
$htmlFolder = $html ? 'html/' : '';
|
$htmlFolder = $html ? 'html/' : '';
|
||||||
|
|
||||||
/* Get email template */
|
/* Get email template */
|
||||||
|
65
api/BusinessLogic/Emails/EmailTemplateRetriever.php
Normal file
65
api/BusinessLogic/Emails/EmailTemplateRetriever.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BusinessLogic\Emails;
|
||||||
|
|
||||||
|
|
||||||
|
class EmailTemplateRetriever {
|
||||||
|
/**
|
||||||
|
* @var $validTemplates EmailTemplate[]
|
||||||
|
*/
|
||||||
|
private $validTemplates;
|
||||||
|
|
||||||
|
function __construct() {
|
||||||
|
$this->validTemplates = array();
|
||||||
|
$this->initializeArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
const FORGOT_TICKET_ID = 0;
|
||||||
|
const NEW_REPLY_BY_STAFF = 1;
|
||||||
|
const NEW_TICKET = 2;
|
||||||
|
const VERIFY_EMAIL = 3;
|
||||||
|
const TICKET_CLOSED = 4;
|
||||||
|
const CATEGORY_MOVED = 5;
|
||||||
|
const NEW_REPLY_BY_CUSTOMER = 6;
|
||||||
|
const NEW_TICKET_STAFF = 7;
|
||||||
|
const TICKET_ASSIGNED_TO_YOU = 8;
|
||||||
|
const NEW_PM = 9;
|
||||||
|
const NEW_NOTE = 10;
|
||||||
|
const RESET_PASSWORD = 11;
|
||||||
|
const CALENDAR_REMINDER = 12;
|
||||||
|
const OVERDUE_TICKET = 13;
|
||||||
|
|
||||||
|
function initializeArray() {
|
||||||
|
if (count($this->validTemplates) > 0) {
|
||||||
|
//-- Map already built
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->validTemplates[self::FORGOT_TICKET_ID] = new EmailTemplate(false, 'forgot_ticket_id');
|
||||||
|
$this->validTemplates[self::NEW_REPLY_BY_STAFF] = new EmailTemplate(false, 'new_reply_by_staff');
|
||||||
|
$this->validTemplates[self::NEW_TICKET] = new EmailTemplate(false, 'new_ticket', 'ticket_received');
|
||||||
|
$this->validTemplates[self::VERIFY_EMAIL] = new EmailTemplate(false, 'verify_email');
|
||||||
|
$this->validTemplates[self::TICKET_CLOSED] = new EmailTemplate(false, 'ticket_closed');
|
||||||
|
$this->validTemplates[self::CATEGORY_MOVED] = new EmailTemplate(true, 'category_moved');
|
||||||
|
$this->validTemplates[self::NEW_REPLY_BY_CUSTOMER] = new EmailTemplate(true, 'new_reply_by_customer');
|
||||||
|
$this->validTemplates[self::NEW_TICKET_STAFF] = new EmailTemplate(true, 'new_ticket_staff');
|
||||||
|
$this->validTemplates[self::TICKET_ASSIGNED_TO_YOU] = new EmailTemplate(true, 'ticket_assigned_to_you');
|
||||||
|
$this->validTemplates[self::NEW_PM] = new EmailTemplate(true, 'new_pm');
|
||||||
|
$this->validTemplates[self::NEW_NOTE] = new EmailTemplate(true, 'new_note');
|
||||||
|
$this->validTemplates[self::RESET_PASSWORD] = new EmailTemplate(true, 'reset_password');
|
||||||
|
$this->validTemplates[self::CALENDAR_REMINDER] = new EmailTemplate(true, 'reset_password');
|
||||||
|
$this->validTemplates[self::OVERDUE_TICKET] = new EmailTemplate(true, 'overdue_ticket');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $templateId
|
||||||
|
* @return EmailTemplate|null
|
||||||
|
*/
|
||||||
|
function getTemplate($templateId) {
|
||||||
|
if (isset($this->validTemplates[$templateId])) {
|
||||||
|
return $this->validTemplates[$templateId];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -1,31 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: mkoch
|
|
||||||
* Date: 2/22/2017
|
|
||||||
* Time: 9:25 PM
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace BusinessLogic\Emails;
|
|
||||||
|
|
||||||
|
|
||||||
class ValidEmailTemplates {
|
|
||||||
static function getValidEmailTemplates() {
|
|
||||||
return array(
|
|
||||||
'forgot_ticket_id' => 'forgot_ticket_id',
|
|
||||||
'new_reply_by_staff' => 'new_reply_by_staff',
|
|
||||||
'new_ticket' => 'ticket_received',
|
|
||||||
'verify_email' => 'verify_email',
|
|
||||||
'ticket_closed' => 'ticket_closed',
|
|
||||||
'category_moved' => 'category_moved',
|
|
||||||
'new_reply_by_customer' => 'new_reply_by_customer',
|
|
||||||
'new_ticket_staff' => 'new_ticket_staff',
|
|
||||||
'ticket_assigned_to_you' => 'ticket_assigned_to_you',
|
|
||||||
'new_pm' => 'new_pm',
|
|
||||||
'new_note' => 'new_note',
|
|
||||||
'reset_password' => 'reset_password',
|
|
||||||
'calendar_reminder' => 'calendar_reminder',
|
|
||||||
'overdue_ticket' => 'overdue_ticket',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
41
api/Tests/BusinessLogic/Emails/EmailSenderHelperTest.php
Normal file
41
api/Tests/BusinessLogic/Emails/EmailSenderHelperTest.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BusinessLogic\Emails;
|
||||||
|
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class EmailSenderHelperTest extends TestCase {
|
||||||
|
/**
|
||||||
|
* @var $emailTemplateParser \PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
private $emailTemplateParser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var $basicEmailSender \PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
private $basicEmailSender;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var $mailgunEmailSender \PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
private $mailgunEmailSender;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var $emailSenderHelper EmailSenderHelper
|
||||||
|
*/
|
||||||
|
private $emailSenderHelper;
|
||||||
|
|
||||||
|
protected function setUp() {
|
||||||
|
$this->emailTemplateParser = $this->createMock(EmailTemplateParser::class);
|
||||||
|
$this->basicEmailSender = $this->createMock(BasicEmailSender::class);
|
||||||
|
$this->mailgunEmailSender = $this->createMock(MailgunEmailSender::class);
|
||||||
|
|
||||||
|
$this->emailSenderHelper = new EmailSenderHelper($this->emailTemplateParser, $this->basicEmailSender,
|
||||||
|
$this->mailgunEmailSender);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItParsesTheTemplateForTheTicket() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user