-
Notifications
You must be signed in to change notification settings - Fork 378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor exception handling of Binary\Loader\ChainLoader
class
#1434
base: 3.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 $loaderIndex; | ||
private LoaderInterface $loaderClass; | ||
|
||
public function __construct(string $loaderIndex, LoaderInterface $loaderClass, NotLoadableException $loaderException) | ||
{ | ||
$this->loaderIndex = $loaderIndex; | ||
$this->loaderClass = $loaderClass; | ||
|
||
parent::__construct($this->compileMessageTxt(), 0, $loaderException); | ||
} | ||
|
||
public function getLoaderIndex(): string | ||
{ | ||
return $this->loaderIndex; | ||
} | ||
|
||
public function getLoaderClass(): LoaderInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not called from outside the class, i think we can drop the method |
||
{ | ||
return $this->loaderClass; | ||
} | ||
|
||
public function getLoaderClassName(): string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but this one should stay as is, it gives you the classname of the loader, so i like the name. if we move reflection to the constructor, this will become a simple getter for |
||
{ | ||
return (new \ReflectionObject($this->getLoaderClass()))->getShortName(); | ||
} | ||
|
||
public function getLoaderException(): string | ||
{ | ||
return $this->getPrevious()->getMessage(); | ||
} | ||
|
||
private function compileMessageTxt(): string | ||
{ | ||
return sprintf('%s=[%s]', $this->getLoaderClassName(), $this->getLoaderIndex()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?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::implodeMappedExceptions(static function (ChainAttemptNotLoadableException $e): string { | ||
return $e->getMessage(); | ||
}, ...$exceptions); | ||
} | ||
|
||
private static function compileLoaderErrorsList(ChainAttemptNotLoadableException ...$exceptions): string | ||
{ | ||
return self::implodeMappedExceptions(static function (ChainAttemptNotLoadableException $e): string { | ||
return sprintf('%s=[%s]', $e->getLoaderClassName(), $e->getLoaderException()); | ||
}, ...$exceptions); | ||
} | ||
|
||
private static function implodeMappedExceptions(\Closure $mapper, ChainAttemptNotLoadableException ...$exceptions): string | ||
{ | ||
return implode(', ', array_map($mapper, $exceptions)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would prefer to call this $loader. $loaderClass suggests its the class name of the loader, but it is the actual instance.
then again, looking at the rest of the code: we do not use the class anywhere afterwards. how about doing the reflection thing in the constructor and have this field be
private string $loaderClassName
?