Skip to content

Commit

Permalink
Merge a440d2d into 5ca445e
Browse files Browse the repository at this point in the history
  • Loading branch information
frasmage committed Oct 10, 2020
2 parents 5ca445e + a440d2d commit fbcb9cc
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 27 deletions.
16 changes: 12 additions & 4 deletions docs/source/variants.rst
Expand Up @@ -152,14 +152,22 @@ Variants can be created from the ``ImageManipulator`` class. This will create a
$variantMedia = ImageManipulator::createImageVariant($originalMedia, 'thumbnail');


Depending on the size of the files and the nature of the manipulations, creating variants may be a time consuming operation. As such, it may be more beneficial to perform the operation asynchronously. The ``CreateImageVariants`` job can be used to easily queue variants to be processed. Multiple variant names can be provided in order to process the creation of several variants as part of the same worker process.
Depending on the size of the files and the nature of the manipulations, creating variants may be a time consuming operation. As such, it may be more beneficial to perform the operation asynchronously. The ``CreateImageVariants`` job can be used to easily queue variants to be processed. A collection of ``Media`` records and multiple variant names can be provided in order to process the creation of several variants as part of the same worker process.

::

<?php
use Plank\Mediable\Jobs\CreateImageVariants;
use Illuminate\Database\Eloquent\Collection;

CreateImageVariants::dispatch($media, ['square', 'bw-square']);
// will produce one variant
CreateImageVariants::dispatch($media, ['square']);

// will produce 4 variants (2 of each media)
CreateImageVariants::dispatch(
new Collection([$media1, $media2]),
['square', 'bw-square']
);

Recreating Variants
^^^^^^^^^^^^^^^^^^^
Expand All @@ -172,7 +180,7 @@ If a variant with the requested variant name already exists for the provided med
$variantMedia = ImageManipulator::createImageVariant($originalMedia, 'thumbnail', true);
CreateImageVariants::dispatch($media, ['square', 'bw-square'], true);

Doing so will cause the original file to be deleted, and a new one created at the specified output destination. The variant record will retain its primary key and any associations, but it attributes will be updated as necessary
Doing so will cause the original file to be deleted, and a new one created at the specified output destination. The variant record will retain its primary key and any associations, but its attributes will be updated as necessary.

Using Variants
--------------
Expand All @@ -191,7 +199,7 @@ However, variants also remember the name of the variant definition and the origi
Original vs. Variants
^^^^^^^^^^^^^^^^^^^^^

An "original" ``Media`` record is one the one that was initially uploaded to the server. A variant is the derivative that was created by manipulating the original. You can distinguish them with these methods
An "original" ``Media`` record is one the one that was initially uploaded to the server. A variant is the derivative that was created by manipulating the original. You can distinguish them with these methods:

::

Expand Down
52 changes: 35 additions & 17 deletions src/Jobs/CreateImageVariants.php
Expand Up @@ -5,6 +5,7 @@
use Carbon\Traits\Serialization;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Plank\Mediable\Exceptions\ImageManipulationException;
Expand All @@ -20,9 +21,9 @@ class CreateImageVariants implements ShouldQueue
*/
private $variantNames;
/**
* @var Media
* @var Collection|Media[]
*/
private $model;
private $models;

/**
* @var bool
Expand All @@ -31,28 +32,31 @@ class CreateImageVariants implements ShouldQueue

/**
* CreateImageVariants constructor.
* @param Media $model
* @param Media|Collection|Media[] $model
* @param string|string[] $variantNames
* @throws ImageManipulationException
*/
public function __construct(Media $model, $variantNames, bool $forceRecreate = false)
public function __construct($models, $variantNames, bool $forceRecreate = false)
{
$models = $this->collect($models);
$variantNames = (array) $variantNames;
$this->validate($model, $variantNames);
$this->validate($models, $variantNames);

$this->variantNames = $variantNames;
$this->model = $model;
$this->models = $models;
$this->forceRecreate = $forceRecreate;
}

public function handle()
{
foreach ($this->getVariantNames() as $variantName) {
$this->getImageManipulator()->createImageVariant(
$this->getModel(),
$variantName,
$this->getForceRecreate()
);
foreach ($this->getModels() as $model) {
foreach ($this->getVariantNames() as $variantName) {
$this->getImageManipulator()->createImageVariant(
$model,
$variantName,
$this->getForceRecreate()
);
}
}
}

Expand All @@ -65,22 +69,24 @@ public function getVariantNames(): array
}

/**
* @return Media
* @return Collection|Media[]
*/
public function getModel(): Media
public function getModels(): Collection
{
return $this->model;
return $this->models;
}

/**
* @param Media $model
* @param array $variantNames
* @throws ImageManipulationException
*/
private function validate(Media $media, array $variantNames): void
private function validate(Collection $models, array $variantNames): void
{
$imageManipulator = $this->getImageManipulator();
$imageManipulator->validateMedia($media);
foreach ($models as $media) {
$imageManipulator->validateMedia($media);
}
foreach ($variantNames as $variantName) {
$imageManipulator->getVariantDefinition($variantName);
}
Expand All @@ -98,4 +104,16 @@ public function getForceRecreate(): bool
{
return $this->forceRecreate;
}

/**
* @param Media|Collection|Media[] $models
* @return bool
*/
private function collect($models): Collection
{
if ($models instanceof Media) {
$models = [$models];
}
return new Collection($models);
}
}
46 changes: 40 additions & 6 deletions tests/Integration/Jobs/CreateImageVariantsTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Plank\Mediable\Tests\Integration\Jobs;

use Illuminate\Database\Eloquent\Collection;
use Plank\Mediable\ImageManipulation;
use Plank\Mediable\ImageManipulator;
use Plank\Mediable\Jobs\CreateImageVariants;
Expand All @@ -12,23 +13,53 @@ class CreateImageVariantsTest extends TestCase
public function test_it_will_trigger_image_manipulation()
{
$model = $this->makeMedia(['aggregate_type' => 'image']);
$variant1 = 'foo';
$variant2 = 'bar';
$variant = 'foo';

$manipulator = $this->createMock(ImageManipulator::class);
$manipulator->expects($this->once())
->method('validateMedia')
->with($model);
$manipulator->expects($this->once())
->method('getVariantDefinition')
->with($variant)
->willReturn($this->createMock(ImageManipulation::class));
$manipulator->expects($this->once())
->method('createImageVariant')
->withConsecutive([$model, $variant, false]);
app()->instance(ImageManipulator::class, $manipulator);

$job = new CreateImageVariants($model, $variant);
$job->handle();
}

public function test_it_will_trigger_image_manipulation_multiple()
{
$model1 = $this->makeMedia(['aggregate_type' => 'image']);
$model2 = $this->makeMedia(['aggregate_type' => 'image']);
$variant1 = 'foo';
$variant2 = 'bar';

$manipulator = $this->createMock(ImageManipulator::class);
$manipulator->expects($this->exactly(2))
->method('validateMedia')
->withConsecutive([$model1],[$model2]);
$manipulator->expects($this->exactly(2))
->method('getVariantDefinition')
->withConsecutive([$variant1], [$variant2])
->willReturn($this->createMock(ImageManipulation::class));
$manipulator->expects($this->exactly(2))
$manipulator->expects($this->exactly(4))
->method('createImageVariant')
->withConsecutive([$model, $variant1, false], [$model, $variant2, false]);
->withConsecutive(
[$model1, $variant1, false],
[$model1, $variant2, false],
[$model2, $variant1, false],
[$model2, $variant2, false]
);
app()->instance(ImageManipulator::class, $manipulator);

$job = new CreateImageVariants($model, [$variant1, $variant2]);
$job = new CreateImageVariants(
new Collection([$model1, $model2]), [$variant1, $variant2]
);
$job->handle();
}

Expand All @@ -48,7 +79,10 @@ public function test_it_will_trigger_image_manipulation_recreate()
->willReturn($this->createMock(ImageManipulation::class));
$manipulator->expects($this->exactly(2))
->method('createImageVariant')
->withConsecutive([$model, $variant1, true], [$model, $variant2, true]);
->withConsecutive(
[$model, $variant1, true],
[$model, $variant2, true]
);
app()->instance(ImageManipulator::class, $manipulator);

$job = new CreateImageVariants($model, [$variant1, $variant2], true);
Expand Down

0 comments on commit fbcb9cc

Please sign in to comment.