Skip to content

Commit

Permalink
handled nullable associations
Browse files Browse the repository at this point in the history
  • Loading branch information
imahmood committed Nov 20, 2023
1 parent 6ede003 commit d855fd1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
32 changes: 32 additions & 0 deletions database/migrations/2023_11_20_110518_update_media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('media', function (Blueprint $table) {
$table->string('model_type')->nullable()->change();
$table->unsignedBigInteger('model_id')->nullable()->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('media', function (Blueprint $table) {
$table->string('model_type')->nullable(false)->change();
$table->unsignedBigInteger('model_id')->nullable(false)->change();
});
}
};
35 changes: 15 additions & 20 deletions src/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Imahmood\FileStorage\Jobs\GeneratePreview;
use Imahmood\FileStorage\Jobs\OptimizeImage;
use Imahmood\FileStorage\Models\Media;
use InvalidArgumentException;

class FileStorage
{
Expand All @@ -31,12 +30,12 @@ public function __construct(
*/
public function create(
MediaTypeInterface $type,
MediaAwareInterface $relatedTo,
?MediaAwareInterface $relatedTo,
UploadedFile $uploadedFile,
): Media {
$media = new Media([
'model_type' => $relatedTo::class,
'model_id' => $relatedTo->getPrimaryKey(),
'model_type' => $relatedTo ? $relatedTo::class : null,
'model_id' => $relatedTo?->getPrimaryKey(),
'type' => $type->identifier(),
]);

Expand All @@ -49,13 +48,13 @@ public function create(
*/
public function update(
MediaTypeInterface $type,
MediaAwareInterface $relatedTo,
?MediaAwareInterface $relatedTo,
Media $media,
?UploadedFile $uploadedFile,
): Media {
$media->fill([
'model_type' => $relatedTo::class,
'model_id' => $relatedTo->getPrimaryKey(),
'model_type' => $relatedTo ? $relatedTo::class : null,
'model_id' => $relatedTo?->getPrimaryKey(),
'type' => $type->identifier(),
]);

Expand All @@ -82,18 +81,14 @@ public function update(
*/
public function updateOrCreate(
MediaTypeInterface $type,
MediaAwareInterface $relatedTo,
?MediaAwareInterface $relatedTo,
?Media $media,
?UploadedFile $uploadedFile,
UploadedFile $uploadedFile,
): Media {
if ($media) {
return $this->update($type, $relatedTo, $media, $uploadedFile);
}

if (! $uploadedFile) {
throw new InvalidArgumentException();
}

return $this->create($type, $relatedTo, $uploadedFile);
}

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

if (! $media->save()) {
if (!$media->save()) {
throw new PersistenceFailedException();
}

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

if (! $media->save()) {
if (!$media->save()) {
throw new PersistenceFailedException();
}

if ($uploadedFile) {
$isUploaded = (bool) $uploadedFile->storeAs($media->dir_relative_path, $media->file_name, [
$isUploaded = (bool)$uploadedFile->storeAs($media->dir_relative_path, $media->file_name, [
'disk' => $this->config->diskName,
]);

if (! $isUploaded) {
if (!$isUploaded) {
throw new PersistenceFailedException();
}

Expand All @@ -155,7 +150,7 @@ protected function persistMedia(Media $media, ?UploadedFile $uploadedFile): Medi
public function delete(Media $media): bool
{
return DB::transaction(function () use ($media) {
if (! $media->delete()) {
if (!$media->delete()) {
return false;
}

Expand All @@ -171,7 +166,7 @@ public function delete(Media $media): bool
protected function deleteDirectory(string $dir): void
{
$isDeleted = Storage::disk($this->config->diskName)->deleteDirectory($dir);
if (! $isDeleted) {
if (!$isDeleted) {
throw new UnableToDeleteDirectoryException(sprintf(
'[FileStorage] Disk: %s, Directory: %s',
$this->config->diskName,
Expand All @@ -186,7 +181,7 @@ protected function deleteDirectory(string $dir): void
protected function deleteFile(array|string $paths): void
{
$isDeleted = Storage::disk($this->config->diskName)->delete($paths);
if (! $isDeleted) {
if (!$isDeleted) {
$paths = is_array($paths) ? implode(', ', $paths) : $paths;

throw new UnableToDeleteFileException(sprintf(
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

/**
* @property int $id
* @property string $model_type
* @property int $model_id
* @property string|null $model_type
* @property int|null $model_id
* @property string $file_name
* @property string|null $preview
* @property int $type
Expand Down

0 comments on commit d855fd1

Please sign in to comment.