TemporaryUpload   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 66
c 1
b 0
f 0
dl 0
loc 160
rs 10
wmc 21

11 Methods

Rating   Name   Duplication   Size   Complexity  
A findByMediaUuidInCurrentSession() 0 13 4
A scopeOld() 0 3 1
A previewManipulation() 0 3 1
A getPreviewManipulation() 0 4 1
A moveMedia() 0 20 3
A findByMediaUuid() 0 20 3
A createForRemoteFile() 0 24 2
A getDiskName() 0 3 1
A prunable() 0 3 1
A registerMediaConversions() 0 13 2
A createForFile() 0 22 2
1
<?php
2
3
namespace Spatie\MediaLibraryPro\Models;
4
5
use Carbon\Carbon;
6
use Closure;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\MassPrunable;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Http\UploadedFile;
11
use Spatie\Image\Manipulations;
12
use Spatie\MediaLibrary\Conversions\Conversion;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\Conversions\Conversion was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Spatie\MediaLibrary\HasMedia;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\HasMedia was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Spatie\MediaLibrary\InteractsWithMedia;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\InteractsWithMedia was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Spatie\MediaLibrary\MediaCollections\Models\Media;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\MediaCollections\Models\Media was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Spatie\MediaLibraryPro\Exceptions\CouldNotAddUpload;
17
use Spatie\MediaLibraryPro\Exceptions\TemporaryUploadDoesNotBelongToCurrentSession;
18
19
class TemporaryUpload extends Model implements HasMedia
20
{
21
    use InteractsWithMedia;
22
    use MassPrunable;
23
24
    protected $guarded = [];
25
26
    public static ?Closure $manipulatePreview = null;
27
28
    public static ?string $disk = null;
29
30
    public function scopeOld(Builder $builder): void
31
    {
32
        $builder->where('created_at', '<=', Carbon::now()->subDay()->toDateTimeString());
33
    }
34
35
    public function registerMediaConversions(Media $media = null): void
36
    {
37
        if (! config('media-library.generate_thumbnails_for_temporary_uploads')) {
38
            return;
39
        }
40
41
        $conversion = $this
42
            ->addMediaConversion('preview')
43
            ->nonQueued();
44
45
        $previewManipulation = $this->getPreviewManipulation();
46
47
        $previewManipulation($conversion);
48
    }
49
50
    public static function previewManipulation(Closure $closure): void
51
    {
52
        static::$manipulatePreview = $closure;
53
    }
54
55
    protected function getPreviewManipulation(): Closure
56
    {
57
        return static::$manipulatePreview ?? function (Conversion $conversion) {
58
            $conversion->fit(Manipulations::FIT_CROP, 300, 300);
59
        };
60
    }
61
62
    protected static function getDiskName(): string
63
    {
64
        return static::$disk ?? config('media-library.disk_name');
65
    }
66
67
    public static function findByMediaUuid(?string $mediaUuid): ?TemporaryUpload
68
    {
69
        $mediaModelClass = config('media-library.media_model');
70
71
        /** @var Media $media */
72
        $media = $mediaModelClass::query()
73
            ->where('uuid', $mediaUuid)
74
            ->first();
75
76
        if (! $media) {
0 ignored issues
show
introduced by
$media is of type Spatie\MediaLibrary\MediaCollections\Models\Media, thus it always evaluated to true.
Loading history...
77
            return null;
78
        }
79
80
        $temporaryUpload = $media->model;
81
82
        if (! $temporaryUpload instanceof TemporaryUpload) {
83
            return null;
84
        }
85
86
        return $temporaryUpload;
87
    }
88
89
    public static function findByMediaUuidInCurrentSession(?string $mediaUuid): ?TemporaryUpload
90
    {
91
        if (! $temporaryUpload = static::findByMediaUuid($mediaUuid)) {
92
            return null;
93
        }
94
95
        if (config('media-library.enable_temporary_uploads_session_affinity', true)) {
96
            if ($temporaryUpload->session_id !== session()->getId()) {
97
                return null;
98
            }
99
        }
100
101
        return $temporaryUpload;
102
    }
103
104
    public static function createForFile(
105
        UploadedFile $file,
106
        string $sessionId,
107
        string $uuid,
108
        string $name
109
    ): self {
110
        /** @var \Spatie\MediaLibraryPro\Models\TemporaryUpload $temporaryUpload */
111
        $temporaryUpload = static::create([
112
            'session_id' => $sessionId,
113
        ]);
114
115
        if (static::findByMediaUuid($uuid)) {
116
            throw CouldNotAddUpload::uuidAlreadyExists();
117
        }
118
119
        $temporaryUpload
120
            ->addMedia($file)
121
            ->setName($name)
122
            ->withProperties(['uuid' => $uuid])
123
            ->toMediaCollection('default', static::getDiskName());
124
125
        return $temporaryUpload->fresh();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $temporaryUpload->fresh() could return the type null which is incompatible with the type-hinted return Spatie\MediaLibraryPro\Models\TemporaryUpload. Consider adding an additional type-check to rule them out.
Loading history...
126
    }
127
128
    public static function createForRemoteFile(
129
        string $file,
130
        string $sessionId,
131
        string $uuid,
132
        string $name,
133
        string $diskName
134
    ): self {
135
        /** @var \Spatie\MediaLibraryPro\Models\TemporaryUpload $temporaryUpload */
136
        $temporaryUpload = static::create([
137
            'session_id' => $sessionId,
138
        ]);
139
140
        if (static::findByMediaUuid($uuid)) {
141
            throw CouldNotAddUpload::uuidAlreadyExists();
142
        }
143
144
        $temporaryUpload
145
            ->addMediaFromDisk($file, $diskName)
146
            ->setName($name)
147
            ->usingFileName($name)
148
            ->withProperties(['uuid' => $uuid])
149
            ->toMediaCollection('default', static::getDiskName());
150
151
        return $temporaryUpload->fresh();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $temporaryUpload->fresh() could return the type null which is incompatible with the type-hinted return Spatie\MediaLibraryPro\Models\TemporaryUpload. Consider adding an additional type-check to rule them out.
Loading history...
152
    }
153
154
    public function moveMedia(HasMedia $toModel, string $collectionName, string $diskName, string $fileName): Media
155
    {
156
        if (config('media-library.enable_temporary_uploads_session_affinity', true)) {
157
            if ($this->session_id !== session()->getId()) {
158
                throw TemporaryUploadDoesNotBelongToCurrentSession::create();
159
            }
160
        }
161
162
        $media = $this->getFirstMedia();
163
164
        $temporaryUploadModel = $media->model;
165
        $uuid = $media->uuid;
166
167
        $newMedia = $media->move($toModel, $collectionName, $diskName, $fileName);
168
169
        $temporaryUploadModel->delete();
170
171
        $newMedia->update(compact('uuid'));
172
173
        return $newMedia;
174
    }
175
176
    public function prunable(): Builder
177
    {
178
        return self::query()->old();
179
    }
180
}
181