Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.00% covered (warning)
70.00%
14 / 20
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
IngestError
70.00% covered (warning)
70.00%
14 / 20
83.33% covered (warning)
83.33%
5 / 6
6.97
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unknownFilename
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 tooLarge
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 disallowedType
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 wrongExtension
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 unsafeSvg
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace 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 */
11class 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    /** @param array<string> $allowed */
47    public static function wrongExtension(?string $ext, array $allowed, string $mime): self
48    {
49        $list = implode(', ', $allowed);
50
51        return new self(
52            "File extension '{$ext}' not allowed for {$mime} (allowed: {$list})",
53            __('media:wrong-extension', ['ext' => (string) $ext, 'allowed' => $list]),
54            $mime,
55        );
56    }
57
58    public static function unsafeSvg(): self
59    {
60        return new self('SVG markup rejected by the sanitizer', __('media:unsafe-svg'));
61    }
62}