unil-lettres /
dilps-tiresias
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Application; |
||
| 6 | |||
| 7 | use Ecodev\Felix\Api\Exception; |
||
| 8 | use Imagick; |
||
| 9 | use ImagickException; |
||
| 10 | use Throwable; |
||
| 11 | |||
| 12 | abstract class FriendlyException |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Any exception thrown by `$callback` will be attempted to be transformed |
||
| 16 | * into a user-friendly version. Then either the user-friendly, or the original, |
||
| 17 | * exception will be re-thrown. |
||
| 18 | * |
||
| 19 | * @template T |
||
| 20 | * |
||
| 21 | * @param callable(): T $callback |
||
| 22 | * |
||
| 23 | * @return T Whatever the callable returned |
||
| 24 | */ |
||
| 25 | 15 | public static function try(callable $callback): mixed |
|
| 26 | { |
||
| 27 | try { |
||
| 28 | 15 | return $callback(); |
|
| 29 | 4 | } catch (Throwable $e) { |
|
| 30 | 4 | $previous = $e->getPrevious(); |
|
| 31 | 4 | if (!($previous instanceof ImagickException)) { |
|
| 32 | 1 | throw $e; |
|
| 33 | } |
||
| 34 | |||
| 35 | 3 | $message = $previous->getMessage(); |
|
| 36 | 3 | if (str_starts_with($message, 'width or height exceeds limit')) { |
|
| 37 | // Your IDE might tell you `RESOURCETYPE_WIDTH` and `RESOURCETYPE_HEIGHT` don't exist, but actually they do exist since imagemagick 6.91 |
||
| 38 | // see https://github.com/Imagick/imagick/blob/45adfb7b1e322eaa6174e88f7d5e27ef20e0596e/imagick_helpers.c#L1672-L1675 |
||
| 39 | 1 | $maxWidth = Utility::formatMetric(Imagick::getResourceLimit(Imagick::RESOURCETYPE_WIDTH)); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
The type
Application\Utility was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 40 | 1 | $maxHeight = Utility::formatMetric(Imagick::getResourceLimit(Imagick::RESOURCETYPE_HEIGHT)); |
|
|
0 ignored issues
–
show
|
|||
| 41 | 1 | self::throw("La dimension maximale de l'image est de $maxWidth x $maxHeight pixels, mais elle a été dépassée.", $e); |
|
| 42 | 2 | } elseif (str_starts_with($message, 'cache resources exhausted')) { |
|
| 43 | 1 | $maxCache = Utility::formatMetric(Imagick::getResourceLimit(Imagick::RESOURCETYPE_DISK)); |
|
| 44 | 1 | self::throw("La taille maximale du cache est de {$maxCache}iB, mais elle a été dépassée.", $e); |
|
| 45 | 1 | } elseif (str_contains($message, 'time limit exceeded')) { |
|
| 46 | 1 | $maxTime = Imagick::getResourceLimit(Imagick::RESOURCETYPE_TIME); |
|
| 47 | 1 | self::throw("Le temps maximum de traitement d'image est de $maxTime secondes, mais il a été dépassé.", $e); |
|
| 48 | } |
||
| 49 | |||
| 50 | throw $e; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | 3 | private static function throw(string $message, Throwable $previous): never |
|
| 55 | { |
||
| 56 | 3 | throw new Exception($message, $previous->getCode(), $previous); |
|
| 57 | } |
||
| 58 | } |
||
| 59 |