Completed
Push — master ( 1c1a7c...9294f6 )
by Pulkit
25s queued 10s
created

ExceptionOccurredMail::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 3
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
introduced by
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 = 'vendor.laravel_alert_notifications.mail';
18
19
    protected $notificationLevel;
20
21
    public function __construct($exception, string $notificationLevel, array $exceptionContext = [])
22
    {
23
        $this->exception         = $exception;
24
        $this->exceptionContext  = $exceptionContext;
25
        $this->notificationLevel = $notificationLevel;
26
    }
27
28
    public function build()
29
    {
30
        $configPrefix = 'laravel_alert_notifications.mail.'.$this->notificationLevel.'.';
31
32
        $from    = config('laravel_alert_notifications.mail.fromAddress');
33
        $to      = config($configPrefix.'toAddress') ?? config('laravel_alert_notifications.mail.toAddress');
34
        $subject = config($configPrefix.'subject') ?? config('laravel_alert_notifications.mail.subject');
35
        $subject = $this->replaceSubjectPlaceholders($subject);
36
37
        $data = [
38
            'exception' => $this->exception,
39
            'context'   => $this->exceptionContext,
40
        ];
41
42
        return $this->subject($subject)->from($from)->to($to)->with($data);
43
    }
44
45
    protected function replaceSubjectPlaceholders(string $subject): string
46
    {
47
        $subject = str_replace('%ExceptionType%', get_class($this->exception), $subject);
48
        $subject = str_replace('%ExceptionMessage%', $this->exception->getMessage(), $subject);
49
        $subject = str_replace('%ExceptionCode%', $this->exception->getCode(), $subject);
50
        $subject = str_replace('%ExceptionLevel%', ucfirst($this->notificationLevel), $subject);
51
52
        return $subject;
53
    }
54
}
55