Skip to content

Commit 11aa6c6

Browse files
committed
cleanup of refactored Liip\ImagineBundle\Binary\Loader\ChainLoader and its related exceptions
1 parent e217f33 commit 11aa6c6

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

src/Binary/Loader/ChainLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public function find($path)
4242
foreach ($this->loaders as $configName => $objectInst) {
4343
try {
4444
return $objectInst->find($path);
45-
} catch (NotLoadableException $e) {
46-
$exceptions[] = new ChainAttemptNotLoadableException($configName, $objectInst, $e);
45+
} catch (NotLoadableException $loaderException) {
46+
$exceptions[] = new ChainAttemptNotLoadableException($configName, $objectInst, $loaderException);
4747
}
4848
}
4949

src/Exception/Binary/Loader/ChainAttemptNotLoadableException.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616
class ChainAttemptNotLoadableException extends NotLoadableException
1717
{
1818
private string $configName;
19-
private string $objectName;
2019
private LoaderInterface $objectInst;
2120

2221
public function __construct(string $configName, LoaderInterface $objectInst, NotLoadableException $loaderException)
2322
{
24-
$this->setupState($configName, $objectInst);
23+
$this->configName = $configName;
24+
$this->objectInst = $objectInst;
2525

26-
parent::__construct(sprintf(
27-
'%s=[%s]', $this->getLoaderObjectName(), $this->getLoaderConfigName()
28-
), 0, $loaderException);
26+
parent::__construct($this->compileFailureText(), 0, $loaderException);
27+
}
28+
29+
public function getLoaderConfigName(): string
30+
{
31+
return $this->configName;
2932
}
3033

3134
public function getLoaderObjectInst(): LoaderInterface
@@ -35,18 +38,16 @@ public function getLoaderObjectInst(): LoaderInterface
3538

3639
public function getLoaderObjectName(): string
3740
{
38-
return $this->objectName;
41+
return (new \ReflectionObject($this->getLoaderObjectInst()))->getShortName();
3942
}
4043

41-
public function getLoaderConfigName(): string
44+
public function getLoaderPriorError(): string
4245
{
43-
return $this->configName;
46+
return $this->getPrevious()->getMessage();
4447
}
4548

46-
private function setupState(string $configName, LoaderInterface $objectInst): void
49+
private function compileFailureText(): string
4750
{
48-
$this->configName = $configName;
49-
$this->objectInst = $objectInst;
50-
$this->objectName = (new \ReflectionObject($objectInst))->getShortName();
51+
return sprintf('%s=[%s]', $this->getLoaderObjectName(), $this->getLoaderConfigName());
5152
}
5253
}

src/Exception/Binary/Loader/ChainNotLoadableException.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,26 @@ public function __construct(string $path, ChainAttemptNotLoadableException ...$e
2020

2121
private static function compileExceptionMessage(string $path, ChainAttemptNotLoadableException ...$exceptions): string
2222
{
23-
$loaderNamedList = array_map(function (ChainAttemptNotLoadableException $e): string {
24-
return $e->getMessage();
25-
}, $exceptions);
26-
27-
$loaderErrorList = array_map(function (ChainAttemptNotLoadableException $e): string {
28-
return sprintf('%s=[%s]', $e->getLoaderObjectName(), $e->getPrevious()->getMessage());
29-
}, $exceptions);
30-
3123
return vsprintf('Source image not resolvable "%s" using "%s" %d loaders (internal exceptions: %s).', [
32-
$path,
33-
self::joinMessageStrings(...$loaderNamedList),
34-
\count($exceptions),
35-
self::joinMessageStrings(...$loaderErrorList),
24+
$path, self::compileLoaderConfigMaps(...$exceptions), \count($exceptions), self::compileLoaderErrorsList(...$exceptions)
3625
]);
3726
}
3827

39-
private static function joinMessageStrings(string ...$messages): string
28+
private static function compileLoaderConfigMaps(ChainAttemptNotLoadableException ...$exceptions): string
4029
{
41-
return implode(', ', $messages);
30+
return self::implodeArrayMappedExceptions(static function (ChainAttemptNotLoadableException $e): string {
31+
return $e->getMessage();
32+
}, ...$exceptions);
33+
}
34+
35+
private static function compileLoaderErrorsList(ChainAttemptNotLoadableException ...$exceptions): string
36+
{
37+
return self::implodeArrayMappedExceptions(static function (ChainAttemptNotLoadableException $e): string {
38+
return sprintf('%s=[%s]', $e->getLoaderObjectName(), $e->getLoaderPriorError());
39+
}, ...$exceptions);
40+
}
41+
42+
private static function implodeArrayMappedExceptions(\Closure $listMapper, ChainAttemptNotLoadableException ...$exceptions): string {
43+
return implode(', ', array_map($listMapper, $exceptions));
4244
}
4345
}

tests/Binary/Loader/ChainLoaderTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
/**
2525
* @covers \Liip\ImagineBundle\Binary\Loader\ChainLoader
26+
* @covers \Liip\ImagineBundle\Exception\Binary\Loader\ChainAttemptNotLoadableException
27+
* @covers \Liip\ImagineBundle\Exception\Binary\Loader\ChainNotLoadableException
2628
*/
2729
class ChainLoaderTest extends AbstractTest
2830
{

0 commit comments

Comments
 (0)