Skip to content

Commit d855fd1

Browse files
committed
handled nullable associations
1 parent 6ede003 commit d855fd1

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
return new class extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*/
14+
public function up(): void
15+
{
16+
Schema::table('media', function (Blueprint $table) {
17+
$table->string('model_type')->nullable()->change();
18+
$table->unsignedBigInteger('model_id')->nullable()->change();
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*/
25+
public function down(): void
26+
{
27+
Schema::table('media', function (Blueprint $table) {
28+
$table->string('model_type')->nullable(false)->change();
29+
$table->unsignedBigInteger('model_id')->nullable(false)->change();
30+
});
31+
}
32+
};

src/FileStorage.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Imahmood\FileStorage\Jobs\GeneratePreview;
1818
use Imahmood\FileStorage\Jobs\OptimizeImage;
1919
use Imahmood\FileStorage\Models\Media;
20-
use InvalidArgumentException;
2120

2221
class FileStorage
2322
{
@@ -31,12 +30,12 @@ public function __construct(
3130
*/
3231
public function create(
3332
MediaTypeInterface $type,
34-
MediaAwareInterface $relatedTo,
33+
?MediaAwareInterface $relatedTo,
3534
UploadedFile $uploadedFile,
3635
): Media {
3736
$media = new Media([
38-
'model_type' => $relatedTo::class,
39-
'model_id' => $relatedTo->getPrimaryKey(),
37+
'model_type' => $relatedTo ? $relatedTo::class : null,
38+
'model_id' => $relatedTo?->getPrimaryKey(),
4039
'type' => $type->identifier(),
4140
]);
4241

@@ -49,13 +48,13 @@ public function create(
4948
*/
5049
public function update(
5150
MediaTypeInterface $type,
52-
MediaAwareInterface $relatedTo,
51+
?MediaAwareInterface $relatedTo,
5352
Media $media,
5453
?UploadedFile $uploadedFile,
5554
): Media {
5655
$media->fill([
57-
'model_type' => $relatedTo::class,
58-
'model_id' => $relatedTo->getPrimaryKey(),
56+
'model_type' => $relatedTo ? $relatedTo::class : null,
57+
'model_id' => $relatedTo?->getPrimaryKey(),
5958
'type' => $type->identifier(),
6059
]);
6160

@@ -82,18 +81,14 @@ public function update(
8281
*/
8382
public function updateOrCreate(
8483
MediaTypeInterface $type,
85-
MediaAwareInterface $relatedTo,
84+
?MediaAwareInterface $relatedTo,
8685
?Media $media,
87-
?UploadedFile $uploadedFile,
86+
UploadedFile $uploadedFile,
8887
): Media {
8988
if ($media) {
9089
return $this->update($type, $relatedTo, $media, $uploadedFile);
9190
}
9291

93-
if (! $uploadedFile) {
94-
throw new InvalidArgumentException();
95-
}
96-
9792
return $this->create($type, $relatedTo, $uploadedFile);
9893
}
9994

@@ -104,7 +99,7 @@ public function updatePreviewName(Media $media, string $fileName): Media
10499
{
105100
$media->preview = $fileName;
106101

107-
if (! $media->save()) {
102+
if (!$media->save()) {
108103
throw new PersistenceFailedException();
109104
}
110105

@@ -122,16 +117,16 @@ protected function persistMedia(Media $media, ?UploadedFile $uploadedFile): Medi
122117
$media->preview = null;
123118
}
124119

125-
if (! $media->save()) {
120+
if (!$media->save()) {
126121
throw new PersistenceFailedException();
127122
}
128123

129124
if ($uploadedFile) {
130-
$isUploaded = (bool) $uploadedFile->storeAs($media->dir_relative_path, $media->file_name, [
125+
$isUploaded = (bool)$uploadedFile->storeAs($media->dir_relative_path, $media->file_name, [
131126
'disk' => $this->config->diskName,
132127
]);
133128

134-
if (! $isUploaded) {
129+
if (!$isUploaded) {
135130
throw new PersistenceFailedException();
136131
}
137132

@@ -155,7 +150,7 @@ protected function persistMedia(Media $media, ?UploadedFile $uploadedFile): Medi
155150
public function delete(Media $media): bool
156151
{
157152
return DB::transaction(function () use ($media) {
158-
if (! $media->delete()) {
153+
if (!$media->delete()) {
159154
return false;
160155
}
161156

@@ -171,7 +166,7 @@ public function delete(Media $media): bool
171166
protected function deleteDirectory(string $dir): void
172167
{
173168
$isDeleted = Storage::disk($this->config->diskName)->deleteDirectory($dir);
174-
if (! $isDeleted) {
169+
if (!$isDeleted) {
175170
throw new UnableToDeleteDirectoryException(sprintf(
176171
'[FileStorage] Disk: %s, Directory: %s',
177172
$this->config->diskName,
@@ -186,7 +181,7 @@ protected function deleteDirectory(string $dir): void
186181
protected function deleteFile(array|string $paths): void
187182
{
188183
$isDeleted = Storage::disk($this->config->diskName)->delete($paths);
189-
if (! $isDeleted) {
184+
if (!$isDeleted) {
190185
$paths = is_array($paths) ? implode(', ', $paths) : $paths;
191186

192187
throw new UnableToDeleteFileException(sprintf(

src/Models/Media.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
/**
1616
* @property int $id
17-
* @property string $model_type
18-
* @property int $model_id
17+
* @property string|null $model_type
18+
* @property int|null $model_id
1919
* @property string $file_name
2020
* @property string|null $preview
2121
* @property int $type

0 commit comments

Comments
 (0)