Passed
Pull Request — master (#20)
by Hilmi Erdem
13:59 queued 07:56
created

TokenNotification::notifiableHasOtpChannels()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 5
rs 10
ccs 0
cts 0
cp 0
crap 12
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
/**
18
 * Class TokenNotification.
19
 */
20
class TokenNotification extends Notification implements ShouldQueue
21
{
22
    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...
23
24
    /**
25
     * The token.
26
     *
27
     * @var OtpToken
28
     */
29
    public OtpToken $token;
30
31
    /**
32
     * TokenNotification constructor.
33
     *
34
     * @param  OtpToken  $token
35 6
     */
36
    public function __construct(OtpToken $token)
37 6
    {
38 6
        $this->token = $token;
39
    }
40
41
    /**
42
     * Get the notification's delivery channels.
43
     *
44
     * @param  mixed  $notifiable
45
     * @return array
46
     */
47 2
    public function via(mixed $notifiable): array
48
    {
49 2
        $channels = $this->notifiableHasOtpChannels($notifiable)
50 1
            ? $notifiable->otpChannels()
51 2
            : 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

51
            : /** @scrutinizer ignore-call */ config('otp.default_channels');
Loading history...
52
53 2
        return is_array($channels)
54 1
            ? $channels
55 2
            : array_map('trim', explode(',', $channels));
56
    }
57
58
    /**
59
     * Get the mail presentation of the notification.
60
     *
61
     * @return MailMessage
62
     */
63 1
    public function toMail(): MailMessage
64
    {
65 1
        $plainText = $this->token->plainText();
66 1
67 1
        return (new MailMessage())
68 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

68
            ->subject(/** @scrutinizer ignore-call */ trans('erdemkeren.otp.token-notification.email.subject'))
Loading history...
69 1
            ->subject(trans('erdemkeren.otp.token-notification.email.greeting'))
70 1
            ->subject(trans('erdemkeren.otp.token-notification.email.line1', $plainText))
71
            ->subject(trans('erdemkeren.otp.token-notification.email.line2', $plainText))
72
            ->subject(trans('erdemkeren.otp.token-notification.email.line3', $plainText));
73
    }
74
75
    /**
76
     * Get the sms presentation of the notification.
77
     *
78 1
     * @return string
79
     */
80
    public function toSms(): string
81 1
    {
82 1
        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

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