WithNotification::notificationUser()   A
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6.1666

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.2222
cc 6
nc 3
nop 0
crap 6.1666
1
<?php
2
3
namespace NovaResourceDynamicExport\Export;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\Relation;
7
use Laravel\Nova\Notifications\NovaNotification;
8
use Laravel\Nova\URL;
9
use NovaResourceDynamicExport\Models\ExportStoredFile;
10
11
trait WithNotification
12
{
13
    use HasDownLoadLink;
14
15
    protected ?string $notificationUserClass = null;
16
    protected ?int $notificationUserId       = null;
17
18
    /**
19
     * Set user data to end notifications for.
20
     *
21
     * @param Model|null $notificationUser
22
     * @return $this
23
     */
24 1
    public function setNotificationUser(?Model $notificationUser = null): static
25
    {
26 1
        $this->notificationUserId    = $notificationUser?->getKey();
27 1
        $this->notificationUserClass = $notificationUser?->getMorphClass();
28
29 1
        return $this;
30
    }
31
32
    /**
33
     * Find user to send notification.
34
     *
35
     * @return Model|null
36
     */
37 1
    public function notificationUser(): ?Model
38
    {
39 1
        if ($this->notificationUserClass && $this->notificationUserId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->notificationUserId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
40 1
            $class = Relation::getMorphedModel($this->notificationUserClass)?:$this->notificationUserClass;
41 1
            if ($class && is_a($class, Model::class, true)) {
42 1
                return $class::query()->find($this->notificationUserId);
43
            }
44
        }
45
46
        return null;
47
    }
48
49
50
    /**
51
     * Send notification to user.
52
     *
53
     * @param ExportStoredFile $fileModel
54
     * @return void
55
     */
56 1
    public function notifyUser(ExportStoredFile $fileModel): void
57
    {
58 1
        $user = $this->notificationUser();
59 1
        if ($user && $fileModel?->exists) {
60 1
            $user->notify(
61 1
                NovaNotification::make()
62 1
                    ->message("File {$fileModel->name} exported")
63 1
                    ->action('Download', URL::remote($this->downloadLink($fileModel->download_link)))
64 1
                    ->icon('download')
65 1
                    ->type('info')
66 1
            );
67
        }
68
    }
69
}
70