Skip to content

Commit

Permalink
WIP HasMedia
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Nov 27, 2023
1 parent 8c21ed1 commit 6cf3ce2
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/MediaCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class MediaCollection
{
public function __construct(
public ?array $acceptedMimeTypes = null,
public Collection $conversions = new Collection()
public bool $single = false,
public bool $public = false,
) {
}
}
20 changes: 15 additions & 5 deletions src/Traits/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Finller\LaravelMedia\Jobs\ConversionJob;
use Finller\LaravelMedia\Media;
use Finller\LaravelMedia\MediaCollection;
use Finller\LaravelMedia\MediaConversion;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
Expand All @@ -27,6 +28,15 @@ public function getMediaCollections(): Collection
return collect([]);
}

/**
* @return Collection<string, MediaConversion>
*/
function getMediaConversions(Media $media): Collection
{
$conversions = collect([]);
return $conversions;
}

public function addMedia(string|UploadedFile $file, string $collection_name = null): static
{
$collection_name ??= config('media.default_collection_name');
Expand All @@ -40,20 +50,20 @@ public function addMedia(string|UploadedFile $file, string $collection_name = nu
return $this;
}

public function dispatchConversions(Media $media, string $collection_name): static
public function dispatchConversions(Media $media): static
{
$collection = $this->getMediaCollections()->get($collection_name);
$conversions = $this->getMediaConversions($media);

if (! $collection) {
if ($conversions->isEmpty()) {
return $this;
}

foreach ($collection->conversions as $conversion) {
foreach ($conversions as $name => $conversion) {
if ($conversion->job instanceof ConversionJob) {
dispatch($conversion->job);
} else {
$job = $conversion->job;
dispatch(new $job($media, $conversion->name));
dispatch(new $job($media, $name));
}
}

Expand Down
11 changes: 10 additions & 1 deletion tests/Feature/HasMediaTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

use FFMpeg\Coordinate\Dimension;
use Finller\LaravelMedia\Casts\GeneratedConversion;
use Finller\LaravelMedia\Database\Factories\MediaFactory;
use Finller\LaravelMedia\Enums\MediaType;
use Finller\LaravelMedia\Media;
use Finller\LaravelMedia\Tests\Models\Test;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

it('retrieve the generated conversion key', function () {
//
$model = (new Test())->save();
});
55 changes: 55 additions & 0 deletions tests/Models/Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Finller\LaravelMedia\Tests\Models;

use Finller\LaravelMedia\Enums\MediaType;
use Finller\LaravelMedia\Jobs\OptimizedImageConversionJob;
use Finller\LaravelMedia\Media;
use Finller\LaravelMedia\MediaCollection;
use Finller\LaravelMedia\MediaConversion;
use Finller\LaravelMedia\Traits\HasMedia;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

/**
* @property ?string $uuid
*/
class Test extends Model
{
use HasMedia;

protected $table = 'tests';

protected $guarded = [];

/**
* @return Collection<string, MediaCollection>
*/
public function getMediaCollections(): Collection
{
return collect([
'files' => new MediaCollection(
single: false,
public: false,
)
]);
}

function getMediaConversions(Media $media): Collection
{

$conversions = collect();

if ($media->type === MediaType::Image) {
$conversions->push(
new MediaConversion(
name: 'optimized',
job: new OptimizedImageConversionJob($media, 'optimized')
)
);
}

return $conversions->keyBy('name');
}
}
13 changes: 10 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Finller\LaravelMedia\LaravelMediaServiceProvider;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Schema\Blueprint;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
Expand All @@ -13,7 +14,7 @@ protected function setUp(): void
parent::setUp();

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'Finller\\LaravelMedia\\Database\\Factories\\'.class_basename($modelName).'Factory'
fn (string $modelName) => 'Finller\\LaravelMedia\\Database\\Factories\\' . class_basename($modelName) . 'Factory'
);
}

Expand All @@ -28,9 +29,15 @@ public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');

$migration = include __DIR__.'/../database/migrations/1_create_media_table.php.stub';
$migration = include __DIR__ . '/../database/migrations/1_create_media_table.php.stub';
$migration->up();
$migration = include __DIR__.'/../database/migrations/2_add_columns_to_media_table.php.stub';
$migration = include __DIR__ . '/../database/migrations/2_add_columns_to_media_table.php.stub';
$migration->up();

$app['db']->connection()->getSchemaBuilder()->create('tests', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->softDeletes();
});
}
}

0 comments on commit 6cf3ce2

Please sign in to comment.