Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
abdosaeedelhassan committed Sep 23, 2024
1 parent 440ffb6 commit 2626ae8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
67 changes: 36 additions & 31 deletions src/Models/EmailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,6 @@ public function __construct(array $attributes = [])
$this->setTableFromConfig();
}

protected static function boot()
{
parent::boot();

// When an email template is updated
static::updated(function ($template) {
self::clearEmailTemplateCache($template->key, $template->language);
});

// When an email template is deleted
static::deleted(function ($template) {
self::clearEmailTemplateCache($template->key, $template->language);
});
}

public function setTableFromConfig()
{
$this->table = config('filament-email-templates.table_name');
Expand All @@ -114,12 +99,6 @@ public static function findEmailByKey($key, $language = null)
});
}

public static function clearEmailTemplateCache($key, $language)
{
$cacheKey = "email_by_key_{$key}_{$language}";
Cache::forget($cacheKey);
}

/**
* @return \Illuminate\Support\Collection
*/
Expand All @@ -128,6 +107,27 @@ public static function getSendToSelectOptions()
return collect(config('emailTemplate.recipients'));
}

protected static function boot()
{
parent::boot();

// When an email template is updated
static::updated(function ($template) {
self::clearEmailTemplateCache($template->key, $template->language);
});

// When an email template is deleted
static::deleted(function ($template) {
self::clearEmailTemplateCache($template->key, $template->language);
});
}

public static function clearEmailTemplateCache($key, $language)
{
$cacheKey = "email_by_key_{$key}_{$language}";
Cache::forget($cacheKey);
}

/**
* @return EmailTemplateFactory
*/
Expand Down Expand Up @@ -181,16 +181,7 @@ public function getBase64EmailPreviewData()
public function getEmailPreviewData()
{
$models = self::createEmailPreviewData();
return [
'user' => $models->user,
'content' => TokenHelper::replace($this->content ?? '', $models),
'subject' => TokenHelper::replace($this->subject ?? '', $models),
'preHeaderText' => TokenHelper::replace($this->preheader ?? '', $models),
'title' => TokenHelper::replace($this->title ?? '', $models),
'theme' => $this->theme->colours,
'logo' => $this->logo,
'language' => $this->language,
];
return self::getEmailData($this, $models);
}

/**
Expand All @@ -212,6 +203,20 @@ public static function createEmailPreviewData()
return $models;
}

public static function getEmailData($template, $model)
{
return [
'user' => $model->user,
'content' => TokenHelper::replace($template->content ?? '', $model),
'subject' => TokenHelper::replace($template->subject ?? '', $model),
'preHeaderText' => TokenHelper::replace($template->preheader ?? '', $model),
'title' => TokenHelper::replace($template->title ?? '', $model),
'theme' => $template->theme->colours,
'logo' => $template->logo,
'language' => $template->language,
];
}

/**
* Efficient method to return requested template locale or default language template in one query
*
Expand Down
26 changes: 10 additions & 16 deletions src/Traits/BuildGenericEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,20 @@ public function build()

if ($this->attachment ?? false) {
$this->attach(
$this->attachment->getPath(),
[
'as' => $this->attachment->filename,
'mime' => $this->attachment->mime_type,
]
$this->attachment->getPath(),
[
'as' => $this->attachment->filename,
'mime' => $this->attachment->mime_type,
]
);
}

$data = [
'content' => TokenHelper::replace($this->emailTemplate->content, $this),
'preHeaderText' => TokenHelper::replace($this->emailTemplate->preheader, $this),
'title' => TokenHelper::replace($this->emailTemplate->title, $this),
'theme' => $this->emailTemplate->theme->colours,
'logo' => $this->emailTemplate->logo,
];
$data = EmailTemplate::getEmailData($this->emailTemplate, $this);

return $this->from($this->emailTemplate->from['email'], $this->emailTemplate->from['name'])
->view($this->emailTemplate->view_path)
->subject(TokenHelper::replace($this->emailTemplate->subject, $this))
->to($this->sendTo)
->with(['data' => $data]);
->view($this->emailTemplate->view_path)
->subject(TokenHelper::replace($this->emailTemplate->subject, $this))
->to($this->sendTo)
->with(['data' => $data]);
}
}

0 comments on commit 2626ae8

Please sign in to comment.