Skip to content

Commit e49602b

Browse files
committed
squashed commits refactoring error handling logic of Liip\ImagineBundle\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
1 parent 6ed7f68 commit e49602b

File tree

4 files changed

+109
-28
lines changed

4 files changed

+109
-28
lines changed

src/Binary/Loader/ChainLoader.php

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111

1212
namespace Liip\ImagineBundle\Binary\Loader;
1313

14+
use Liip\ImagineBundle\Exception\Binary\Loader\ChainAttemptNotLoadableException;
15+
use Liip\ImagineBundle\Exception\Binary\Loader\ChainNotLoadableException;
1416
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
1517

1618
class ChainLoader implements LoaderInterface
1719
{
1820
/**
19-
* @var LoaderInterface[]
21+
* @var array<string, LoaderInterface>
2022
*/
2123
private array $loaders;
2224

2325
/**
24-
* @param LoaderInterface[] $loaders
26+
* @param array<string, LoaderInterface> $loaders
2527
*/
2628
public function __construct(array $loaders)
2729
{
@@ -37,36 +39,14 @@ public function find($path)
3739
{
3840
$exceptions = [];
3941

40-
foreach ($this->loaders as $loader) {
42+
foreach ($this->loaders as $configName => $loader) {
4143
try {
4244
return $loader->find($path);
43-
} catch (\Exception $e) {
44-
$exceptions[$e->getMessage()] = $loader;
45+
} catch (NotLoadableException $loaderException) {
46+
$exceptions[] = new ChainAttemptNotLoadableException($configName, $loader, $loaderException);
4547
}
4648
}
4749

48-
throw new NotLoadableException(self::getLoaderExceptionMessage($path, $exceptions, $this->loaders));
49-
}
50-
51-
/**
52-
* @param array<string, LoaderInterface> $exceptions
53-
* @param array<string, LoaderInterface> $loaders
54-
*/
55-
private static function getLoaderExceptionMessage(string $path, array $exceptions, array $loaders): string
56-
{
57-
$loaderMessages = array_map(static function (string $name, LoaderInterface $loader) {
58-
return sprintf('%s=[%s]', (new \ReflectionObject($loader))->getShortName(), $name);
59-
}, array_keys($loaders), $loaders);
60-
61-
$exceptionMessages = array_map(static function (string $message, LoaderInterface $loader) {
62-
return sprintf('%s=[%s]', (new \ReflectionObject($loader))->getShortName(), $message);
63-
}, array_keys($exceptions), $exceptions);
64-
65-
return vsprintf('Source image not resolvable "%s" using "%s" %d loaders (internal exceptions: %s).', [
66-
$path,
67-
implode(', ', $loaderMessages),
68-
\count($loaders),
69-
implode(', ', $exceptionMessages),
70-
]);
50+
throw new ChainNotLoadableException($path, ...$exceptions);
7151
}
7252
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the `liip/LiipImagineBundle` project.
5+
*
6+
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7+
*
8+
* For the full copyright and license information, please view the LICENSE.md
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Liip\ImagineBundle\Exception\Binary\Loader;
13+
14+
use Liip\ImagineBundle\Binary\Loader\LoaderInterface;
15+
16+
final class ChainAttemptNotLoadableException extends NotLoadableException
17+
{
18+
private string $configName;
19+
private LoaderInterface $loaderInst;
20+
21+
public function __construct(string $configName, LoaderInterface $loaderInst, NotLoadableException $loaderException)
22+
{
23+
$this->configName = $configName;
24+
$this->loaderInst = $loaderInst;
25+
26+
parent::__construct($this->compileFailureText(), 0, $loaderException);
27+
}
28+
29+
public function getLoaderConfigName(): string
30+
{
31+
return $this->configName;
32+
}
33+
34+
public function getLoaderObjectInst(): LoaderInterface
35+
{
36+
return $this->loaderInst;
37+
}
38+
39+
public function getLoaderObjectName(): string
40+
{
41+
return (new \ReflectionObject($this->getLoaderObjectInst()))->getShortName();
42+
}
43+
44+
public function getLoaderPriorError(): string
45+
{
46+
return $this->getPrevious()->getMessage();
47+
}
48+
49+
private function compileFailureText(): string
50+
{
51+
return sprintf('%s=[%s]', $this->getLoaderObjectName(), $this->getLoaderConfigName());
52+
}
53+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the `liip/LiipImagineBundle` project.
5+
*
6+
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7+
*
8+
* For the full copyright and license information, please view the LICENSE.md
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Liip\ImagineBundle\Exception\Binary\Loader;
13+
14+
final class ChainNotLoadableException extends NotLoadableException
15+
{
16+
public function __construct(string $path, ChainAttemptNotLoadableException ...$exceptions)
17+
{
18+
parent::__construct(self::compileExceptionMessage($path, ...$exceptions));
19+
}
20+
21+
private static function compileExceptionMessage(string $path, ChainAttemptNotLoadableException ...$exceptions): string
22+
{
23+
return vsprintf('Source image not resolvable "%s" using "%s" %d loaders (internal exceptions: %s).', [
24+
$path, self::compileLoaderConfigMaps(...$exceptions), \count($exceptions), self::compileLoaderErrorsList(...$exceptions),
25+
]);
26+
}
27+
28+
private static function compileLoaderConfigMaps(ChainAttemptNotLoadableException ...$exceptions): string
29+
{
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 $mapper, ChainAttemptNotLoadableException ...$exceptions): string
43+
{
44+
return implode(', ', array_map($mapper, $exceptions));
45+
}
46+
}

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)