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

WithNotification   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 35
ccs 0
cts 18
cp 0
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setNotificationUser() 0 6 1
A notificationUser() 0 8 2
A notifyUser() 0 10 3
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