2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Abstracts;
|
|
|
|
|
2020-01-09 14:55:57 +03:00
|
|
|
use App\Models\Common\EmailTemplate;
|
2019-11-16 10:21:14 +03:00
|
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
|
|
|
use Illuminate\Notifications\Notification as BaseNotification;
|
|
|
|
|
|
|
|
abstract class Notification extends BaseNotification
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a notification instance.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->queue = 'high';
|
|
|
|
$this->delay = config('queue.connections.database.delay');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the notification's channels.
|
|
|
|
*
|
|
|
|
* @param mixed $notifiable
|
|
|
|
* @return array|string
|
|
|
|
*/
|
|
|
|
public function via($notifiable)
|
|
|
|
{
|
|
|
|
return ['mail', 'database'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the mail representation of the notification.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Notifications\Messages\MailMessage
|
|
|
|
*/
|
|
|
|
public function initMessage()
|
|
|
|
{
|
2020-01-09 14:55:57 +03:00
|
|
|
$template = EmailTemplate::alias($this->template)->first();
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = (new MailMessage)
|
|
|
|
->from(config('mail.from.address'), config('mail.from.name'))
|
2020-01-09 14:55:57 +03:00
|
|
|
->subject($this->getSubject($template))
|
|
|
|
->view('partials.email.body', ['body' => $this->getBody($template)]);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
2020-01-09 14:55:57 +03:00
|
|
|
public function getSubject($template)
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2020-01-09 14:55:57 +03:00
|
|
|
return $this->replaceTags($template->subject);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 14:55:57 +03:00
|
|
|
public function getBody($template)
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2020-01-09 14:55:57 +03:00
|
|
|
return $this->replaceTags($template->body);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function replaceTags($content)
|
|
|
|
{
|
2020-02-27 09:56:08 +03:00
|
|
|
$pattern = $this->getTagsPattern();
|
|
|
|
$replacement = $this->applyQuote($this->getTagsReplacement());
|
|
|
|
|
|
|
|
return $this->revertQuote(preg_replace($pattern, $replacement, $content));
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getTagsPattern()
|
|
|
|
{
|
|
|
|
$pattern = [];
|
|
|
|
|
|
|
|
foreach($this->getTags() as $tag) {
|
|
|
|
$pattern[] = "/" . $tag . "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pattern;
|
|
|
|
}
|
2020-02-27 09:56:08 +03:00
|
|
|
|
|
|
|
public function getTags()
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTagsReplacement()
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function applyQuote($vars)
|
|
|
|
{
|
|
|
|
$new_vars = [];
|
|
|
|
|
|
|
|
foreach ($vars as $var) {
|
|
|
|
$new_vars[] = preg_quote($var);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $new_vars;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function revertQuote($content)
|
|
|
|
{
|
|
|
|
return str_replace('\\', '', $content);
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|