Issues (3)

src/Mail/ExceptionOccurredMail.php (1 issue)

Severity
1
<?php
2
3
namespace Kevincobain2000\LaravelAlertNotifications\Mail;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Mail\Mailable;
7
use Illuminate\Queue\InteractsWithQueue;
8
use Illuminate\Queue\SerializesModels;
9
10
class ExceptionOccurredMail extends Mailable
11
{
12
    use InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by Kevincobain2000\LaravelA...l\ExceptionOccurredMail: $id, $class
Loading history...
13
14
    public $exception;
15
    public $exceptionContext;
16
17
    public $view = 'laravel_alert_notifications::mail';
18
    // public $view = 'vendor.laravel_alert_notifications.mail';
19
20
    protected $notificationLevel;
21
22
    public function __construct($exception, string $notificationLevel, array $exceptionContext = [])
23
    {
24
        $this->exception         = $exception;
25
        $this->exceptionContext  = $exceptionContext;
26
        $this->notificationLevel = $notificationLevel;
27
    }
28
29
    public function build()
30
    {
31
        $configPrefix = 'laravel_alert_notifications.mail.'.$this->notificationLevel.'.';
32
33
        $from    = config('laravel_alert_notifications.mail.fromAddress');
34
        $to      = config($configPrefix.'toAddress') ?? config('laravel_alert_notifications.mail.toAddress');
35
        $subject = config($configPrefix.'subject') ?? config('laravel_alert_notifications.mail.subject');
36
        $subject = $this->replaceSubjectPlaceholders($subject);
37
38
        $data = [
39
            'exception' => $this->exception,
40
            'context'   => $this->exceptionContext
41
        ];
42
43
        return $this->subject($subject)->from($from)->to($to)->with($data);
44
    }
45
46
    protected function replaceSubjectPlaceholders(string $subject): string
47
    {
48
        $subject = str_replace('%ExceptionType%', get_class($this->exception), $subject);
49
        $subject = str_replace('%ExceptionMessage%', $this->exception->getMessage(), $subject);
50
        $subject = str_replace('%ExceptionCode%', $this->exception->getCode(), $subject);
51
        $subject = str_replace('%ExceptionLevel%', ucfirst($this->notificationLevel), $subject);
52
53
        return $subject;
54
    }
55
}
56