SendResetPasswordEmail::handle()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.9332
cc 4
nc 3
nop 2
crap 4
1
<?php
2
3
namespace NovaThinKit\Nova\Actions;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Password;
8
use Laravel\Nova\Actions\Action;
9
use Laravel\Nova\Actions\DestructiveAction;
10
use Laravel\Nova\Fields\ActionFields;
11
use Laravel\Nova\Http\Requests\NovaRequest;
12
use ThinkStudio\HtmlField\Html;
13
14
class SendResetPasswordEmail extends DestructiveAction
15
{
16
    use ForUser;
17
18
    public $showInline   = true;
19
    public $showOnIndex  = false;
20
    public $showOnDetail = true;
21
22 8
    public function __construct(
23
        protected string $broker,
24
        ?string          $name = null,
25
        ?string          $confirmText = null,
26
    ) {
27 8
        if ($name) {
28 8
            $this->name = $name;
29
        }
30 8
        if ($confirmText) {
31 8
            $this->confirmText = $confirmText;
32
        }
33
    }
34
35
    /**
36
     * Perform the action on the given models.
37
     *
38
     * @param \Laravel\Nova\Fields\ActionFields $fields
39
     * @param \Illuminate\Support\Collection $models
40
     *
41
     * @return mixed
42
     */
43 3
    public function handle(ActionFields $fields, Collection $models)
44
    {
45 3
        $model = $models->first();
46
        if (
47 3
            $model instanceof Model &&
48 3
            ($modelId = $this->findId($model))
49
        ) {
50 2
            $status = Password::broker($this->broker)->sendResetLink([
51 2
                $model->getKeyName() => $modelId,
52 2
            ]);
53
54 2
            if ($status !== Password::RESET_LINK_SENT) {
55 1
                return Action::danger(__($status));
56
            }
57
58 1
            return Action::message(__($status));
59
        }
60
61 1
        return Action::danger(trans('nova-thinkit::action.reset-password-notification.error.user-not-found'));
62
    }
63
64
    /**
65
     * Get the fields available on the action.
66
     *
67
     * @return array
68
     */
69 4
    public function fields(NovaRequest $request)
70
    {
71 4
        return [
72 4
            Html::make(trans('nova-thinkit::action.reset-password-notification.warning.label'), function () {
73 2
                return $this->confirmText;
74 4
            }),
75 4
        ];
76
    }
77
}
78