NewDevice::toMail()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 17
rs 9.8666
1
<?php
2
3
namespace KeyShang\AuthenticationLog\Notifications;
4
5
use Exception;
6
use Illuminate\Bus\Queueable;
7
use Illuminate\Contracts\Queue\ShouldQueue;
8
use Illuminate\Notifications\Messages\MailMessage;
9
use Illuminate\Notifications\Notification;
10
use Illuminate\Support\Carbon;
11
use KeyShang\AuthenticationLog\AuthenticationLog;
12
13
class NewDevice extends Notification implements ShouldQueue
14
{
15
    use Queueable;
16
17
    /**
18
     * The authentication log.
19
     */
20
    public AuthenticationLog $authenticationLog;
21
22
    /**
23
     * Create a new notification instance.
24
     *
25
     * @param  AuthenticationLog  $authenticationLog
26
     * @return void
27
     */
28
    public function __construct(AuthenticationLog $authenticationLog)
29
    {
30
        $this->authenticationLog = $authenticationLog;
31
    }
32
33
    /**
34
     * Get the notification's delivery channels.
35
     *
36
     * @param  mixed  $notifiable
37
     */
38
    public function via($notifiable): array
39
    {
40
        return $notifiable->notifyAuthenticationLogVia();
41
    }
42
43
    /**
44
     * Get the mail representation of the notification.
45
     *
46
     * @param  mixed  $notifiable
47
     *
48
     * @throws Exception
49
     */
50
    public function toMail($notifiable): MailMessage
51
    {
52
        /** @var Carbon $loginAt */
53
        $loginAt = $this->authenticationLog->login_at;
54
        $loginAt = $loginAt->setTimezone('UTC');
55
        $subject = trans('authentication-log::new_device.subject', ['app' => config('app.name')]);
56
        if (gettype($subject) !== 'string') {
57
            throw new Exception('Translate subject error');
58
        }
59
60
        return (new MailMessage)
61
            ->subject($subject)
62
            ->markdown('authentication-log::emails.new_device', [
63
                'account' => $notifiable,
64
                'loginAt' => $loginAt.' UTC',
65
                'ipAddress' => $this->authenticationLog->ip_address,
66
                'browser' => $this->authenticationLog->user_agent,
67
            ]);
68
    }
69
}
70