Skip to content

Commit

Permalink
squashed commits refactoring error handling logic of Liip\ImagineBund…
Browse files Browse the repository at this point in the history
…le\Binary\Loader\ChainLoader:

- refactored Liip\ImagineBundle\Binary\Loader\ChainLoader error handling logic to exception classes instead of prior half-baked and confusing handling inside loader inself
- testing alternate ChainNotLoadableException::compileExceptionMessage() implementation
- cleanup of refactored Liip\ImagineBundle\Binary\Loader\ChainLoader and its related exceptions
- ran php-cs-fixer on prior ChainLoader related changes
- updates per reviews from @dbu and @franmomu
  • Loading branch information
robfrawley committed Nov 25, 2021
1 parent 6ed7f68 commit e49602b
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 28 deletions.
36 changes: 8 additions & 28 deletions src/Binary/Loader/ChainLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@

namespace Liip\ImagineBundle\Binary\Loader;

use Liip\ImagineBundle\Exception\Binary\Loader\ChainAttemptNotLoadableException;
use Liip\ImagineBundle\Exception\Binary\Loader\ChainNotLoadableException;
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;

class ChainLoader implements LoaderInterface
{
/**
* @var LoaderInterface[]
* @var array<string, LoaderInterface>
*/
private array $loaders;

/**
* @param LoaderInterface[] $loaders
* @param array<string, LoaderInterface> $loaders
*/
public function __construct(array $loaders)
{
Expand All @@ -37,36 +39,14 @@ public function find($path)
{
$exceptions = [];

foreach ($this->loaders as $loader) {
foreach ($this->loaders as $configName => $loader) {
try {
return $loader->find($path);
} catch (\Exception $e) {
$exceptions[$e->getMessage()] = $loader;
} catch (NotLoadableException $loaderException) {
$exceptions[] = new ChainAttemptNotLoadableException($configName, $loader, $loaderException);
}
}

throw new NotLoadableException(self::getLoaderExceptionMessage($path, $exceptions, $this->loaders));
}

/**
* @param array<string, LoaderInterface> $exceptions
* @param array<string, LoaderInterface> $loaders
*/
private static function getLoaderExceptionMessage(string $path, array $exceptions, array $loaders): string
{
$loaderMessages = array_map(static function (string $name, LoaderInterface $loader) {
return sprintf('%s=[%s]', (new \ReflectionObject($loader))->getShortName(), $name);
}, array_keys($loaders), $loaders);

$exceptionMessages = array_map(static function (string $message, LoaderInterface $loader) {
return sprintf('%s=[%s]', (new \ReflectionObject($loader))->getShortName(), $message);
}, array_keys($exceptions), $exceptions);

return vsprintf('Source image not resolvable "%s" using "%s" %d loaders (internal exceptions: %s).', [
$path,
implode(', ', $loaderMessages),
\count($loaders),
implode(', ', $exceptionMessages),
]);
throw new ChainNotLoadableException($path, ...$exceptions);
}
}
53 changes: 53 additions & 0 deletions src/Exception/Binary/Loader/ChainAttemptNotLoadableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Liip\ImagineBundle\Exception\Binary\Loader;

use Liip\ImagineBundle\Binary\Loader\LoaderInterface;

final class ChainAttemptNotLoadableException extends NotLoadableException
{
private string $configName;
private LoaderInterface $loaderInst;

public function __construct(string $configName, LoaderInterface $loaderInst, NotLoadableException $loaderException)
{
$this->configName = $configName;
$this->loaderInst = $loaderInst;

parent::__construct($this->compileFailureText(), 0, $loaderException);
}

public function getLoaderConfigName(): string
{
return $this->configName;
}

public function getLoaderObjectInst(): LoaderInterface
{
return $this->loaderInst;
}

public function getLoaderObjectName(): string
{
return (new \ReflectionObject($this->getLoaderObjectInst()))->getShortName();
}

public function getLoaderPriorError(): string
{
return $this->getPrevious()->getMessage();
}

private function compileFailureText(): string
{
return sprintf('%s=[%s]', $this->getLoaderObjectName(), $this->getLoaderConfigName());
}
}
46 changes: 46 additions & 0 deletions src/Exception/Binary/Loader/ChainNotLoadableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Liip\ImagineBundle\Exception\Binary\Loader;

final class ChainNotLoadableException extends NotLoadableException
{
public function __construct(string $path, ChainAttemptNotLoadableException ...$exceptions)
{
parent::__construct(self::compileExceptionMessage($path, ...$exceptions));
}

private static function compileExceptionMessage(string $path, ChainAttemptNotLoadableException ...$exceptions): string
{
return vsprintf('Source image not resolvable "%s" using "%s" %d loaders (internal exceptions: %s).', [
$path, self::compileLoaderConfigMaps(...$exceptions), \count($exceptions), self::compileLoaderErrorsList(...$exceptions),
]);
}

private static function compileLoaderConfigMaps(ChainAttemptNotLoadableException ...$exceptions): string
{
return self::implodeArrayMappedExceptions(static function (ChainAttemptNotLoadableException $e): string {
return $e->getMessage();
}, ...$exceptions);
}

private static function compileLoaderErrorsList(ChainAttemptNotLoadableException ...$exceptions): string
{
return self::implodeArrayMappedExceptions(static function (ChainAttemptNotLoadableException $e): string {
return sprintf('%s=[%s]', $e->getLoaderObjectName(), $e->getLoaderPriorError());
}, ...$exceptions);
}

private static function implodeArrayMappedExceptions(\Closure $mapper, ChainAttemptNotLoadableException ...$exceptions): string
{
return implode(', ', array_map($mapper, $exceptions));
}
}
2 changes: 2 additions & 0 deletions tests/Binary/Loader/ChainLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

/**
* @covers \Liip\ImagineBundle\Binary\Loader\ChainLoader
* @covers \Liip\ImagineBundle\Exception\Binary\Loader\ChainAttemptNotLoadableException
* @covers \Liip\ImagineBundle\Exception\Binary\Loader\ChainNotLoadableException
*/
class ChainLoaderTest extends AbstractTest
{
Expand Down

0 comments on commit e49602b

Please sign in to comment.