Passed
Pull Request — master (#20)
by Hilmi Erdem
10:02
created

TokenNotification   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 76
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A via() 0 9 3
A notifiableHasOtpChannels() 0 5 3
A toSms() 0 3 1
A __construct() 0 3 1
A toMail() 0 10 1
1
<?php
2
3
/*
4
 * @copyright 2021 Hilmi Erdem KEREN
5
 * @license MIT
6
 */
7
8
namespace Erdemkeren\Otp;
9
10
use Illuminate\Bus\Queueable;
11
use Illuminate\Contracts\Queue\ShouldQueue;
12
use Illuminate\Notifications\Messages\MailMessage;
13
use Illuminate\Notifications\Notification;
14
use Illuminate\Support\Traits\Macroable;
15
use function is_array;
16
17
class TokenNotification extends Notification implements ShouldQueue
18
{
19
    use Queueable, Macroable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by Erdemkeren\Otp\TokenNotification.
Loading history...
20
21
    /**
22
     * The token.
23
     *
24
     * @var OtpToken
25
     */
26
    public OtpToken $token;
27
28
    /**
29
     * TokenNotification constructor.
30
     *
31
     * @param  OtpToken  $token
32
     */
33
    public function __construct(OtpToken $token)
34
    {
35 6
        $this->token = $token;
36
    }
37 6
38 6
    /**
39
     * Get the notification's delivery channels.
40
     *
41
     * @param  mixed  $notifiable
42
     * @return array
43
     */
44
    public function via(mixed $notifiable): array
45
    {
46
        $channels = $this->notifiableHasOtpChannels($notifiable)
47 2
            ? $notifiable->otpChannels()
48
            : config('otp.default_channels');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
            : /** @scrutinizer ignore-call */ config('otp.default_channels');
Loading history...
49 2
50 1
        return is_array($channels)
51 2
            ? $channels
52
            : array_map('trim', explode(',', $channels));
53 2
    }
54 1
55 2
    /**
56
     * Get the mail presentation of the notification.
57
     *
58
     * @return MailMessage
59
     */
60
    public function toMail(): MailMessage
61
    {
62
        $plainText = $this->token->plainText();
63 1
64
        return (new MailMessage())
65 1
            ->subject(trans('erdemkeren.otp.token-notification.email.subject'))
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            ->subject(/** @scrutinizer ignore-call */ trans('erdemkeren.otp.token-notification.email.subject'))
Loading history...
66 1
            ->subject(trans('erdemkeren.otp.token-notification.email.greeting'))
67 1
            ->subject(trans('erdemkeren.otp.token-notification.email.line1', $plainText))
68 1
            ->subject(trans('erdemkeren.otp.token-notification.email.line2', $plainText))
69 1
            ->subject(trans('erdemkeren.otp.token-notification.email.line3', $plainText));
70 1
    }
71
72
    /**
73
     * Get the sms presentation of the notification.
74
     *
75
     * @return string
76
     */
77
    public function toSms(): string
78 1
    {
79
        return trans('erdemkeren.otp.token-notification.sms', $this->token->plainText());
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        return /** @scrutinizer ignore-call */ trans('erdemkeren.otp.token-notification.sms', $this->token->plainText());
Loading history...
80
    }
81 1
82 1
    /**
83
     * Determine if the notifiable has otp channels or not.
84
     *
85
     * @param  mixed  $notifiable
86
     * @return bool
87
     */
88
    private function notifiableHasOtpChannels(mixed $notifiable): bool
89
    {
90
        return ! is_null($notifiable)
91
        && method_exists($notifiable, 'otpChannels')
92
        && ! empty($notifiable->otpChannels());
93
    }
94
}
95