Passed
Push — main ( ac25fd...00e2bd )
by Yaroslav
14:14
created

WithNotification::setNotificationUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace NovaExportConfiguration\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 NovaExportConfiguration\Models\ExportStoredFile;
10
11
trait WithNotification
12
{
13
    use HasDownLoadLink;
14
15
    protected ?string $notificationUserClass = null;
16
    protected ?int $notificationUserId       = null;
17
18
    public function setNotificationUser(?Model $notificationUser = null): static
19
    {
20
        $this->notificationUserId    = $notificationUser?->getKey();
21
        $this->notificationUserClass = $notificationUser?->getMorphClass();
22
23
        return $this;
24
    }
25
26
    public function notificationUser(): ?Model
27
    {
28
        $class = Relation::getMorphedModel($this->notificationUserClass);
29
        if ($class) {
30
            return $class::find($this->notificationUserId);
31
        }
32
33
        return null;
34
    }
35
36
    public function notifyUser(ExportStoredFile $fileModel): void
37
    {
38
        $user = $this->notificationUser();
39
        if ($user && $fileModel?->exists) {
40
            $user->notify(
41
                NovaNotification::make()
42
                    ->message("File {$fileModel->name} exported")
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on NovaExportConfiguration\Models\ExportStoredFile. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
43
                    ->action('Download', URL::remote($this->downloadLink(route(config('nova-export-configuration.defaults.download_route'), $fileModel->path))))
0 ignored issues
show
Bug introduced by
The property path does not seem to exist on NovaExportConfiguration\Models\ExportStoredFile. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
44
                    ->icon('download')
45
                    ->type('info')
46
            );
47
        }
48
    }
49
}
50