Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Email system to builder pattern #436

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions lib/Email/EmailBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

class EmailBuilder
{
private array $subjectTranslationParams;
private string $languageCode;
private string $template;
private array $smartyValues = [];
private EmailAddress $from;
/**
*
* @var EmailAddress[]
*/
private array $to = [];
/**
*
* @var EmailAddress[]
*/
private array $cc = [];
/**
*
* @var EmailAddress[]
*/
private array $bcc = [];
/**
*
* @var StringAttachment[]
*/
private array $attachments = [];

/**
*
* @param string[] $args
* @return EmailBuilder
*/
public function SubjectTranslation(string $key, array $args = []): EmailBuilder
{
$this->subjectTranslationParams =
[
'key' => $key,
'args' => implode(',', $args)
];
return $this;
}

public function LanguageCode(string $languageCode): EmailBuilder
{
$this->languageCode = $languageCode;
return $this;
}

public function Template(string $template): EmailBuilder
{
$this->template = $template;
return $this;
}

public function FromAddress(EmailAddress $from): EmailBuilder
{
$this->from = $from;
return $this;
}

public function From(string $email, string $name): EmailBuilder
{
return $this->FromAddress(new EmailAddress($email, $name));
}

public function AddToAddress(EmailAddress $to): EmailBuilder
{
$this->to[] = $to;
return $this;
}

public function AddTo(string $email, string $name): EmailBuilder
{
return $this->AddToAddress(new EmailAddress($email, $name));
}

public function AddCC(EmailAddress $cc): EmailBuilder
{
$this->cc[] = $cc;
return $this;
}

public function AddBCC(EmailAddress $bcc): EmailBuilder
{
$this->bcc[] = $bcc;
return $this;
}

public function AddStringAttachment(string $filename, string $contents): EmailBuilder
{
$this->attachments[] = new StringAttachment($filename, $contents);
return $this;
}

public function Set($var, $value): EmailBuilder
{
$this->smartyValues[] = [$var, $value];
return $this;
}

public function Build(): RenderedMessage
{
$enforceCustomTemplate = Configuration::Instance()->GetKey(ConfigKeys::ENFORCE_CUSTOM_MAIL_TEMPLATE, new BooleanConverter());
$email = new SmartyPage($resources);
$languageCode = $this->languageCode;
if (!empty($languageCode)) {
$resources->SetLanguage($languageCode);
$email->assign('CurrentLanguage', $languageCode);
}

$email->assign('ScriptUrl', Configuration::Instance()->GetScriptUrl());
$email->assign('Charset', $resources->Charset);
$email->assign('AppTitle', (empty($appTitle) ? 'LibreBooking' : $appTitle));

// ^^ copied

foreach($this->smartyValues as $pair)
$email->assign($pair[0], $pair[1]);

$header = $email->fetch('Email/emailheader.tpl');
$body = $email->FetchLocalized($this->template, $enforceCustomTemplate);
$footer = $email->fetch('Email/emailfooter.tpl');

$subject = $email->SmartyTranslate($this->subjectTranslationParams, $email);

$from = $this->from ?? new EmailAddress(Configuration::Instance()->GetAdminEmail(), Configuration::Instance()->GetKey(ConfigKeys::ADMIN_EMAIL_NAME));
$replyTo = $from;

$charset = $resources->Charset;

return new RenderedMessage(
$charset,
$subject,
$from,
$replyTo,
$this->to,
$this->cc,
$this->bcc,
$header . $body . $footer,
$this->attachments
);
}
}
56 changes: 12 additions & 44 deletions lib/Email/Messages/AccountActivationEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,22 @@

require_once(ROOT_DIR . 'lib/Email/namespace.php');

class AccountActivationEmail extends EmailMessage
class AccountActivationEmail
{
/**
* @var User
*/
private $user;

/**
* @var string
*/
private $activationCode;

public function __construct(User $user, $activationCode)
{
$this->user = $user;
$this->activationCode = $activationCode;
parent::__construct($user->Language());
}

/**
* @return array|EmailAddress[]|EmailAddress
*/
public function To()
{
return new EmailAddress($this->user->EmailAddress(), $this->user->FullName());
}

/**
* @return string
*/
public function Subject()
{
return $this->Translate('ActivateYourAccount');
}

/**
* @return string
*/
public function Body()
public static function BuilderFor(User $user, $activationCode): EmailBuilder
{
$activationUrl = new Url(Configuration::Instance()->GetScriptUrl());
$activationUrl
->Add(Pages::ACTIVATION)
->AddQueryString(QueryStringKeys::ACCOUNT_ACTIVATION_CODE, $this->activationCode);

$this->Set('FirstName', $this->user->FirstName());
$this->Set('EmailAddress', $this->user->EmailAddress());
$this->Set('ActivationUrl', $activationUrl);
return $this->FetchTemplate('AccountActivation.tpl');
->AddQueryString(QueryStringKeys::ACCOUNT_ACTIVATION_CODE, $activationCode);

return (new EmailBuilder)
->SubjectTranslation('ActivateYourAccount')
->AddTo($user->EmailAddress(), $user->FullName())
->Template('AccountActivation.tpl')
->LanguageCode($user->Language())
->Set('FirstName', $user->FirstName())
->Set('EmailAddress', $user->EmailAddress())
->Set('ActivationUrl', $activationUrl);
}
}
74 changes: 22 additions & 52 deletions lib/Email/Messages/AccountCreationEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,35 @@
require_once(ROOT_DIR . 'lib/Email/namespace.php');
require_once(ROOT_DIR . 'Domain/namespace.php');

class AccountCreationEmail extends EmailMessage
class AccountCreationEmail
{
/**
* @var User
*/
private $user;

/**
* @var null|UserSession
*/
private $userSession;

public function __construct(User $user, $userSession = null)
{
$this->user = $user;
$this->userSession = $userSession;
parent::__construct(Configuration::Instance()->GetKey(ConfigKeys::LANGUAGE));
}

/**
* @return array|EmailAddress[]|EmailAddress
*/
public function To()
public static function AccountCreationEmail(User $user, $userSession = null): EmailBuilder
{
$builder = new EmailBuilder;
$userRepo = new UserRepository();
$admins = $userRepo->GetApplicationAdmins();
$emails = [];
foreach ($admins as $admin) {
$emails[] = new EmailAddress($admin->EmailAddress, $admin->FullName);
$builder->AddTo($admin->EmailAddress, $admin->FullName);
}
return $emails;
}

/**
* @return string
*/
public function Subject()
{
return $this->Translate('UserAdded');
}

/**
* @return string
*/
public function Body()
{
$this->Set('To', Configuration::Instance()->GetKey(ConfigKeys::ADMIN_EMAIL_NAME) ? Configuration::Instance()
->GetKey(ConfigKeys::ADMIN_EMAIL_NAME) : 'Administrator');
$this->Set('FullName', $this->user->FullName());
$this->Set('EmailAddress', $this->user->EmailAddress());
$this->Set('Phone', $this->user->GetAttribute(UserAttribute::Phone));
$this->Set('Organization', $this->user->GetAttribute(UserAttribute::Organization));
$this->Set('Position', $this->user->GetAttribute(UserAttribute::Position));

$this->Set('CreatedBy', '');
if ($this->userSession != null && $this->userSession->UserId != $this->user->Id()) {
$this->Set('CreatedBy', new FullName($this->userSession->FirstName, $this->userSession->LastName));

$builder
->SubjectTranslation('UserAdded')
->Template('AccountCreation.tpl')
->LanguageCode(Configuration::Instance()->GetKey(ConfigKeys::LANGUAGE))
->Set('To', Configuration::Instance()->GetKey(ConfigKeys::ADMIN_EMAIL_NAME)
? Configuration::Instance()->GetKey(ConfigKeys::ADMIN_EMAIL_NAME)
: 'Administrator')
->Set('FullName', $user->FullName())
->Set('EmailAddress', $user->EmailAddress())
->Set('Phone', $user->GetAttribute(UserAttribute::Phone))
->Set('Organization', $user->GetAttribute(UserAttribute::Organization))
->Set('Position', $user->GetAttribute(UserAttribute::Position))
->Set('CreatedBy', '');

if ($userSession != null && $userSession->UserId != $user->Id()) {
$builder->Set('CreatedBy', new FullName($userSession->FirstName, $userSession->LastName));
}

return $this->FetchTemplate('AccountCreation.tpl');
return $builder;
}
}
80 changes: 22 additions & 58 deletions lib/Email/Messages/AccountCreationForUserEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,29 @@

require_once(ROOT_DIR . 'lib/Email/namespace.php');

class AccountCreationForUserEmail extends EmailMessage
class AccountCreationForUserEmail
{
/**
* @var User
*/
private $user;

/**
* @var null|UserSession
*/
private $userSession;

/**
* @var string
*/
private $password;

public function __construct(User $user, $password, $userSession = null)
public static function BuilderFor(User $user, $password, $userSession = null): EmailBuilder
{
$this->user = $user;
$this->userSession = $userSession;
$this->password = $password;
parent::__construct(Configuration::Instance()->GetKey(ConfigKeys::LANGUAGE));
}

/**
* @return array|EmailAddress[]|EmailAddress
*/
public function To()
{
return new EmailAddress($this->user->EmailAddress(), $this->user->FullName());
}

/**
* @return string
*/
public function Subject()
{
return $this->Translate('GuestAccountCreatedSubject', [Configuration::Instance()->GetKey(ConfigKeys::APP_TITLE)]);
}

/**
* @return string
*/
public function Body()
{
$this->Set('FullName', $this->user->FullName());
$this->Set('EmailAddress', $this->user->EmailAddress());
$this->Set('Phone', $this->user->GetAttribute(UserAttribute::Phone));
$this->Set('Organization', $this->user->GetAttribute(UserAttribute::Organization));
$this->Set('Position', $this->user->GetAttribute(UserAttribute::Position));
$this->Set('Password', $this->password);
$this->Set('AppTitle', Configuration::Instance()->GetKey(ConfigKeys::APP_TITLE));
$this->Set('ScriptUrl', Configuration::Instance()->GetScriptUrl());
$this->Set('CreatedBy', '');
if ($this->userSession != null && $this->userSession->UserId != $this->user->Id()) {
$this->Set('CreatedBy', new FullName($this->userSession->FirstName, $this->userSession->LastName));
}

return $this->FetchTemplate('AccountCreationForUser.tpl');
$builder = new EmailBuilder();
$builder
->SubjectTranslation('GuestAccountCreatedSubject', [Configuration::Instance()->GetKey(ConfigKeys::APP_TITLE)])
->AddTo($user->EmailAddress(), $user->FullName())
->Template('AccountCreationForUser.tpl')
->LanguageCode(Configuration::Instance()->GetKey(ConfigKeys::LANGUAGE))
->Set('FullName', $user->FullName())
->Set('EmailAddress', $user->EmailAddress())
->Set('Phone', $user->GetAttribute(UserAttribute::Phone))
->Set('Organization', $user->GetAttribute(UserAttribute::Organization))
->Set('Position', $user->GetAttribute(UserAttribute::Position))
->Set('Password', $password)
->Set('AppTitle', Configuration::Instance()->GetKey(ConfigKeys::APP_TITLE))
->Set('ScriptUrl', Configuration::Instance()->GetScriptUrl())
->Set('CreatedBy', '');

if ($userSession != null && $userSession->UserId != $user->Id()) {
$builder->Set('CreatedBy', new FullName($userSession->FirstName, $userSession->LastName));
}
return $builder;
}
}
Loading
Loading