Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
65.00% |
13 / 20 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| IngestError | |
65.00% |
13 / 20 |
|
66.67% |
4 / 6 |
7.54 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| unknownFilename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| tooLarge | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| disallowedType | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| wrongExtension | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| unsafeSvg | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Exception; |
| 6 | |
| 7 | /** |
| 8 | * A rejected asset ingest. The exception message is plain English for CLI |
| 9 | * callers and logs; `userMessage` is the translated one an HTTP caller returns. |
| 10 | */ |
| 11 | class IngestError extends RuntimeException |
| 12 | { |
| 13 | public function __construct( |
| 14 | string $message, |
| 15 | public readonly string $userMessage, |
| 16 | public readonly ?string $mime = null, |
| 17 | ) { |
| 18 | parent::__construct($message); |
| 19 | } |
| 20 | |
| 21 | public static function unknownFilename(): self |
| 22 | { |
| 23 | return new self('No usable filename', __('media:upload-failed')); |
| 24 | } |
| 25 | |
| 26 | public static function tooLarge(int $bytes, int $maxSize): self |
| 27 | { |
| 28 | $size = number_format(($bytes / 1024) / 1024, 2, '.', ''); |
| 29 | $allowed = number_format(($maxSize / 1024) / 1024, 2, '.', ''); |
| 30 | |
| 31 | return new self( |
| 32 | "File too large: {$size} MiB ({$allowed} MiB allowed)", |
| 33 | __('media:too-large', ['size' => $size, 'allowed' => $allowed]), |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | public static function disallowedType(string $mime): self |
| 38 | { |
| 39 | return new self( |
| 40 | "File type not allowed: {$mime}", |
| 41 | __('media:disallowed-type', ['type' => $mime]), |
| 42 | $mime, |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | public static function wrongExtension(?string $ext, array $allowed, string $mime): self |
| 47 | { |
| 48 | $list = implode(', ', $allowed); |
| 49 | |
| 50 | return new self( |
| 51 | "File extension '{$ext}' not allowed for {$mime} (allowed: {$list})", |
| 52 | __('media:wrong-extension', ['ext' => (string) $ext, 'allowed' => $list]), |
| 53 | $mime, |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | public static function unsafeSvg(): self |
| 58 | { |
| 59 | return new self('SVG markup rejected by the sanitizer', __('media:unsafe-svg')); |
| 60 | } |
| 61 | } |