Passed
Pull Request — master (#1300)
by Curtis
05:45
created

HasFile::upload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace App\Traits\enso\files;
4
5
use Illuminate\Database\Eloquent\Relations\Relation;
6
use Illuminate\Http\File as IlluminateFile;
7
use Illuminate\Http\UploadedFile;
8
use Illuminate\Support\Facades\App;
9
use Illuminate\Support\Facades\Config;
10
use Illuminate\Support\Facades\Storage;
11
use App\Models\User;
12
use LaravelEnso\Files\App\Models\File;
13
use App\Service\enso\files\Files;
14
use Symfony\Component\HttpFoundation\StreamedResponse;
15
16
trait HasFile
17
{
18
    public function file(): Relation
19
    {
20
        return $this->morphOne(File::class, 'attachable');
0 ignored issues
show
Bug introduced by
It seems like morphOne() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        return $this->/** @scrutinizer ignore-call */ morphOne(File::class, 'attachable');
Loading history...
21
    }
22
23
    public function inline(): StreamedResponse
24
    {
25
        return (new Files($this))->inline();
0 ignored issues
show
Bug introduced by
$this of type App\Traits\enso\files\HasFile is incompatible with the type App\Contracts\enso\files\Attachable expected by parameter $attachable of App\Service\enso\files\Files::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        return (new Files(/** @scrutinizer ignore-type */ $this))->inline();
Loading history...
26
    }
27
28
    public function download(): StreamedResponse
29
    {
30
        return (new Files($this))->download();
0 ignored issues
show
Bug introduced by
$this of type App\Traits\enso\files\HasFile is incompatible with the type App\Contracts\enso\files\Attachable expected by parameter $attachable of App\Service\enso\files\Files::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
        return (new Files(/** @scrutinizer ignore-type */ $this))->download();
Loading history...
31
    }
32
33
    public function temporaryLink(): string
34
    {
35
        return $this->file->temporaryLink();
36
    }
37
38
    public function attach(IlluminateFile $file, string $originalName, ?User $user): void
39
    {
40
        (new Files($this))->attach($file, $originalName, $user);
0 ignored issues
show
Bug introduced by
$this of type App\Traits\enso\files\HasFile is incompatible with the type App\Contracts\enso\files\Attachable expected by parameter $attachable of App\Service\enso\files\Files::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        (new Files(/** @scrutinizer ignore-type */ $this))->attach($file, $originalName, $user);
Loading history...
41
    }
42
43
    public function upload(UploadedFile $file): void
44
    {
45
        (new Files($this))
0 ignored issues
show
Bug introduced by
$this of type App\Traits\enso\files\HasFile is incompatible with the type App\Contracts\enso\files\Attachable expected by parameter $attachable of App\Service\enso\files\Files::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        (new Files(/** @scrutinizer ignore-type */ $this))
Loading history...
46
            ->mimeTypes($this->mimeTypes())
47
            ->extensions($this->extensions())
48
            ->optimize($this->optimizeImages())
49
            ->resize($this->resizeImages())
50
            ->upload($file);
51
    }
52
53
    public function folder(): string
54
    {
55
        if (App::environment('testing')) {
56
            $directory = Config::get('enso.files.testingFolder');
57
58
            if (! Storage::has($directory)) {
59
                Storage::makeDirectory($directory);
60
            }
61
62
            return $directory;
63
        }
64
65
        return $this->folder;
66
    }
67
68
    public function publicPath(): ?string
69
    {
70
        return $this->file
71
            ? "{$this->folder()}/{$this->file->saved_name}"
72
            : null;
73
    }
74
75
    public function mimeTypes(): array
76
    {
77
        return property_exists($this, 'mimeTypes')
78
            ? $this->mimeTypes
79
            : [];
80
    }
81
82
    public function extensions(): array
83
    {
84
        return property_exists($this, 'extensions')
85
            ? $this->extensions
86
            : [];
87
    }
88
89
    public function resizeImages(): array
90
    {
91
        return property_exists($this, 'resizeImages')
92
            ? $this->resizeImages
93
            : [];
94
    }
95
96
    public function optimizeImages(): bool
97
    {
98
        return property_exists($this, 'optimizeImages')
99
            ? $this->optimizeImages
100
            : false;
101
    }
102
103
    protected static function bootHasFile()
104
    {
105
        self::deleting(fn ($model) => (new Files($model))->delete());
106
    }
107
}
108