Passed
Pull Request — master (#36)
by Key
04:46
created

NewDevice::toSlack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 12
rs 9.9332
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
     * @throws Exception
48
     */
49
    public function toMail($notifiable): MailMessage
50
    {
51
        /** @var Carbon $loginAt */
52
        $loginAt = $this->authenticationLog->login_at;
53
        $loginAt = $loginAt->setTimezone('UTC');
54
        $subject = trans('authentication-log::new_device.subject', ['app' => config('app.name')]);
55
        if (gettype($subject) !== 'string') {
56
            throw new Exception('Translate subject error');
57
        }
58
59
        return (new MailMessage)
60
            ->subject($subject)
61
            ->markdown('authentication-log::emails.new_device', [
62
                'account' => $notifiable,
63
                'loginAt' => $loginAt.' UTC',
64
                'ipAddress' => $this->authenticationLog->ip_address,
65
                'browser' => $this->authenticationLog->user_agent,
66
            ]);
67
    }
68
}
69