Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| DocumentFile | |
100.00% |
5 / 5 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| formatSize | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Modules\Documents\Domain\Model; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | /** |
| 12 | * Domain model representing an attached file belonging to a Document. |
| 13 | * |
| 14 | * @package App\Modules\Documents\Domain\Model |
| 15 | */ |
| 16 | final readonly class DocumentFile |
| 17 | { |
| 18 | /** |
| 19 | * DocumentFile constructor. |
| 20 | * |
| 21 | * @param int $id Unique file attachment identifier. |
| 22 | * @param int $documentId Parent document record identifier. |
| 23 | * @param int|null $versionId Optional document version identifier snapshot link. |
| 24 | * @param string $fileName Original uploaded file name. |
| 25 | * @param string $filePath Relative or absolute storage path on disk. |
| 26 | * @param int $fileSize File size in bytes. |
| 27 | * @param string $fileExtension Lowercase file extension without leading dot. |
| 28 | * @param string $mimeType MIME content type (e.g. application/pdf). |
| 29 | * @param int $sortOrder Display ordering index. |
| 30 | * @param int $createdBy User identifier who uploaded the file. |
| 31 | * @param string $createdAt Upload creation timestamp. |
| 32 | */ |
| 33 | public function __construct( |
| 34 | public int $id, |
| 35 | public int $documentId, |
| 36 | public ?int $versionId, |
| 37 | public string $fileName, |
| 38 | public string $filePath, |
| 39 | public int $fileSize, |
| 40 | public string $fileExtension, |
| 41 | public string $mimeType, |
| 42 | public int $sortOrder = 10, |
| 43 | public int $createdBy = 1, |
| 44 | public string $createdAt = '', |
| 45 | ) { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Formats file size in human-readable units (B, KB, MB, GB). |
| 50 | * |
| 51 | * @return string Human-readable size string. |
| 52 | */ |
| 53 | public function formatSize(): string |
| 54 | { |
| 55 | return match (true) { |
| 56 | $this->fileSize >= 1073741824 => number_format($this->fileSize / 1073741824, 2) . ' GB', |
| 57 | $this->fileSize >= 1048576 => number_format($this->fileSize / 1048576, 2) . ' MB', |
| 58 | $this->fileSize >= 1024 => number_format($this->fileSize / 1024, 1) . ' KB', |
| 59 | default => $this->fileSize . ' B', |
| 60 | }; |
| 61 | } |
| 62 | } |