Skip to content

Commit

Permalink
cleanup of refactored Liip\ImagineBundle\Binary\Loader\ChainLoader an…
Browse files Browse the repository at this point in the history
…d its related exceptions
  • Loading branch information
robfrawley committed Nov 23, 2021
1 parent e217f33 commit 11aa6c6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/Binary/Loader/ChainLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public function find($path)
foreach ($this->loaders as $configName => $objectInst) {
try {
return $objectInst->find($path);
} catch (NotLoadableException $e) {
$exceptions[] = new ChainAttemptNotLoadableException($configName, $objectInst, $e);
} catch (NotLoadableException $loaderException) {
$exceptions[] = new ChainAttemptNotLoadableException($configName, $objectInst, $loaderException);
}
}

Expand Down
25 changes: 13 additions & 12 deletions src/Exception/Binary/Loader/ChainAttemptNotLoadableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@
class ChainAttemptNotLoadableException extends NotLoadableException
{
private string $configName;
private string $objectName;
private LoaderInterface $objectInst;

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

parent::__construct(sprintf(
'%s=[%s]', $this->getLoaderObjectName(), $this->getLoaderConfigName()
), 0, $loaderException);
parent::__construct($this->compileFailureText(), 0, $loaderException);
}

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

public function getLoaderObjectInst(): LoaderInterface
Expand All @@ -35,18 +38,16 @@ public function getLoaderObjectInst(): LoaderInterface

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

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

private function setupState(string $configName, LoaderInterface $objectInst): void
private function compileFailureText(): string
{
$this->configName = $configName;
$this->objectInst = $objectInst;
$this->objectName = (new \ReflectionObject($objectInst))->getShortName();
return sprintf('%s=[%s]', $this->getLoaderObjectName(), $this->getLoaderConfigName());
}
}
30 changes: 16 additions & 14 deletions src/Exception/Binary/Loader/ChainNotLoadableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@ public function __construct(string $path, ChainAttemptNotLoadableException ...$e

private static function compileExceptionMessage(string $path, ChainAttemptNotLoadableException ...$exceptions): string
{
$loaderNamedList = array_map(function (ChainAttemptNotLoadableException $e): string {
return $e->getMessage();
}, $exceptions);

$loaderErrorList = array_map(function (ChainAttemptNotLoadableException $e): string {
return sprintf('%s=[%s]', $e->getLoaderObjectName(), $e->getPrevious()->getMessage());
}, $exceptions);

return vsprintf('Source image not resolvable "%s" using "%s" %d loaders (internal exceptions: %s).', [
$path,
self::joinMessageStrings(...$loaderNamedList),
\count($exceptions),
self::joinMessageStrings(...$loaderErrorList),
$path, self::compileLoaderConfigMaps(...$exceptions), \count($exceptions), self::compileLoaderErrorsList(...$exceptions)
]);
}

private static function joinMessageStrings(string ...$messages): string
private static function compileLoaderConfigMaps(ChainAttemptNotLoadableException ...$exceptions): string
{
return implode(', ', $messages);
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 $listMapper, ChainAttemptNotLoadableException ...$exceptions): string {
return implode(', ', array_map($listMapper, $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 11aa6c6

Please sign in to comment.