Skip to content

Commit

Permalink
wip: upgrade ImageDispenser to use Invervention 3
Browse files Browse the repository at this point in the history
  • Loading branch information
reliq committed Mar 30, 2024
1 parent 0c7a232 commit 883ce60
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 312 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"orchestra/testbench": "^9.0",
"phpro/grumphp": "^2.5",
"phpspec/prophecy-phpunit": "^2.0",
"symplify/easy-coding-standard": ">=8.2",
"phpunit/phpunit": "^11.0",
"yieldstudio/grumphp-laravel-pint": "^1.0"
},
"autoload": {
Expand Down
44 changes: 7 additions & 37 deletions src/Concern/Guide.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Illuminate\Http\Request;
use ReliqArts\GuidedImage\Contract\GuidedImage;
use ReliqArts\GuidedImage\Contract\ImageDispenser;
use ReliqArts\GuidedImage\Demand\Dummy;
use ReliqArts\GuidedImage\Demand\Resize;
use ReliqArts\GuidedImage\Demand\Thumbnail;
use ReliqArts\GuidedImage\Result;
Expand All @@ -36,59 +35,30 @@ public function emptyCache(ImageDispenser $imageDispenser): JsonResponse
);
}

/**
* @param mixed $width
* @param mixed $height
* @param mixed $aspect
* @param mixed $upSize
*/
public function resized(
ImageDispenser $imageDispenser,
Request $request,
GuidedImage $guidedImage,
$width,
$height,
$aspect = true,
$upSize = false
mixed $width,
mixed $height,
mixed $aspect = true,
mixed $upSize = false
): Response {
$demand = new Resize($request, $guidedImage, $width, $height, $aspect, $upSize);

return $imageDispenser->getResizedImage($demand);
}

/**
* @param mixed $method
* @param mixed $width
* @param mixed $height
*/
public function thumb(
ImageDispenser $imageDispenser,
Request $request,
GuidedImage $guidedImage,
$method,
$width,
$height
mixed $method,
mixed $width,
mixed $height
): Response {
$demand = new Thumbnail($request, $guidedImage, $method, $width, $height);

return $imageDispenser->getImageThumbnail($demand);
}

/**
* @param mixed $width
* @param mixed $height
* @param mixed $color
* @param mixed $fill
*/
public function dummy(
ImageDispenser $imageDispenser,
$width,
$height,
$color = null,
$fill = null
): Response {
$demand = new Dummy($width, $height, $color, $fill);

return $imageDispenser->getDummyImage($demand);
}
}
7 changes: 1 addition & 6 deletions src/Contract/ImageDemand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,5 @@ public function getWidth(): ?int;

public function getHeight(): ?int;

public function returnObject(): bool;

/**
* @param mixed $value
*/
public function isValueConsideredNull($value): bool;
public function isValueConsideredNull(mixed $value): bool;
}
5 changes: 3 additions & 2 deletions src/Contract/ImageDispenser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ReliqArts\GuidedImage\Demand\Dummy;
use ReliqArts\GuidedImage\Demand\Resize;
use ReliqArts\GuidedImage\Demand\Thumbnail;
use RuntimeException;
use Symfony\Component\HttpFoundation\Response;

interface ImageDispenser
Expand All @@ -27,9 +28,9 @@ public function getResizedImage(Resize $demand);
/**
* Get dummy Guided Image.
*
* @return Image|Response
* @throws RuntimeException
*/
public function getDummyImage(Dummy $demand);
public function getDummyImage(Dummy $demand): Image;

public function emptyCache(): bool;
}
37 changes: 4 additions & 33 deletions src/Demand/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,19 @@ final class Dummy extends Image
{
public const DEFAULT_COLOR = 'eefefe';

/**
* @var string
*/
private $color;

/**
* @var mixed
*/
private $filling;

/**
* Dummy constructor.
*
* @param mixed $width
* @param mixed $height
* @param mixed $color
* @param mixed $filling
* @param null $returnObject
*/
public function __construct(
$width,
$height,
$color = null,
$filling = null,
$returnObject = null
mixed $width,
mixed $height,
private readonly mixed $color = null
) {
parent::__construct($width, $height, $returnObject);

$this->color = $color;
$this->filling = $filling;
parent::__construct($width, $height);
}

public function getColor(): string
{
return $this->isValueConsideredNull($this->color) ? self::DEFAULT_COLOR : $this->color;
}

/**
* @return mixed
*/
public function fill()
{
return $this->isValueConsideredNull($this->filling) ? null : $this->filling;
}
}
30 changes: 7 additions & 23 deletions src/Demand/ExistingImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,13 @@

abstract class ExistingImage extends Image
{
/**
* @var Request
*/
private Request $request;

/**
* @var GuidedImage
*/
private GuidedImage $guidedImage;

/**
* ExistingImage constructor.
*
* @param mixed $width
* @param mixed $height
* @param mixed $returnObject
*/
public function __construct(Request $request, GuidedImage $guidedImage, $width, $height, $returnObject = null)
{
parent::__construct($width, $height, $returnObject);

$this->request = $request;
$this->guidedImage = $guidedImage;
public function __construct(
private readonly Request $request,
private readonly GuidedImage $guidedImage,
mixed $width,
mixed $height
) {
parent::__construct($width, $height);
}

final public function getRequest(): Request
Expand Down
41 changes: 4 additions & 37 deletions src/Demand/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,21 @@

abstract class Image implements ImageDemand
{
/**
* @var mixed
*/
private $width;

/**
* @var mixed
*/
private $height;

/**
* @var mixed
*/
private $returnObject;

/**
* Image constructor.
*
* @param mixed $width
* @param mixed $height
* @param mixed $returnObject
*/
public function __construct($width, $height, $returnObject = null)
public function __construct(private readonly mixed $width, private readonly mixed $height)
{
$this->width = $width;
$this->height = $height;
$this->returnObject = $returnObject;
}

final public function getWidth(): ?int
{
return $this->isValueConsideredNull($this->width) ? null : (int)$this->width;
return $this->isValueConsideredNull($this->width) ? null : (int) $this->width;
}

final public function getHeight(): ?int
{
return $this->isValueConsideredNull($this->height) ? null : (int)$this->height;
}

final public function returnObject(): bool
{
return !$this->isValueConsideredNull($this->returnObject);
return $this->isValueConsideredNull($this->height) ? null : (int) $this->height;
}

/**
* @param mixed $value
*/
final public function isValueConsideredNull($value): bool
final public function isValueConsideredNull(mixed $value): bool
{
return in_array($value, static::NULLS, true);
}
Expand Down
43 changes: 13 additions & 30 deletions src/Demand/Resize.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,30 @@ final class Resize extends ExistingImage
{
public const ROUTE_TYPE_NAME = 'resize';

/**
* @var mixed
*/
private $maintainAspectRatio;

/**
* @var mixed
*/
private $allowUpSizing;

/**
* Resized constructor.
*
* @param mixed $width
* @param mixed $height
* @param mixed $aspect
* @param mixed $upSize
* @param mixed $returnObject
*/
public function __construct(
Request $request,
GuidedImage $guidedImage,
$width,
$height,
$aspect = true,
$upSize = null,
$returnObject = null
mixed $width,
mixed $height,
private readonly mixed $maintainAspectRatio = true,
private readonly mixed $allowUpSizing = null,
private readonly bool $returnObject = false
) {
parent::__construct($request, $guidedImage, $width, $height, $returnObject);

$this->maintainAspectRatio = $aspect;
$this->allowUpSizing = $upSize;
parent::__construct($request, $guidedImage, $width, $height);
}

public function maintainAspectRatio(): bool
{
return !$this->isValueConsideredNull($this->maintainAspectRatio);
return ! $this->isValueConsideredNull($this->maintainAspectRatio);
}

public function allowUpSizing(): bool
{
return !$this->isValueConsideredNull($this->allowUpSizing);
return ! $this->isValueConsideredNull($this->allowUpSizing);
}

public function returnObject(): bool
{
return $this->returnObject;
}
}
35 changes: 18 additions & 17 deletions src/Demand/Thumbnail.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,45 @@ final class Thumbnail extends ExistingImage
public const ROUTE_TYPE_NAME = 'thumb';

private const METHOD_CROP = 'crop';

private const METHOD_COVER = 'cover';

private const METHOD_FIT = 'fit';
private const METHODS = [self::METHOD_CROP, self::METHOD_FIT];

/**
* @var string
*/
private string $method;
private const METHODS = [self::METHOD_CROP, self::METHOD_FIT, self::METHOD_COVER];

/**
* Thumbnail constructor.
*
* @param mixed $width
* @param mixed $height
* @param mixed $returnObject
*/
public function __construct(
Request $request,
GuidedImage $guidedImage,
string $method,
private readonly string $method,
$width,
$height,
$returnObject = null
private readonly bool $returnObject = false
) {
parent::__construct($request, $guidedImage, $width, $height, $returnObject);

$this->method = $method;
parent::__construct($request, $guidedImage, $width, $height);
}

/**
* @codeCoverageIgnore
*/
public function getMethod(): string
{
// since intervention/image v3; fit() was replaced by cover() and other methods
// see https://image.intervention.io/v3/introduction/upgrade
if ($this->method === self::METHOD_FIT) {
return self::METHOD_COVER;
}

return $this->method;
}

public function isValid(): bool
{
return in_array($this->method, self::METHODS, true);
}

public function returnObject(): bool
{
return $this->returnObject;
}
}
Loading

0 comments on commit 883ce60

Please sign in to comment.