PipelineDocumentDTO.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 3

Klassen 1

Funktionen 5

Versionen 1

Code

<?php

declare(strict_types=1);

namespace Domain\DTO;

use Domain\ValueObject\DocumentStatus;

// @responsibility: DTO für Pipeline-Dokument-Daten

final readonly class PipelineDocumentDTO
{
    public function __construct(
        public int $id,
        public int $pipelineId,
        public string $filename,
        public string $filepath,
        public string $mimeType,
        public int $fileSize,
        public DocumentStatus $status,
        public ?string $errorMessage,
        public int $chunkCount,
        public \DateTimeImmutable $createdAt,
        public ?\DateTimeImmutable $processedAt,
    ) {
    }

    /**
     * Create from database row.
     *
     * @param array<string, mixed> $row
     */
    public static function fromDatabaseRow(array $row): self
    {
        $id = isset($row['id']) ? (int) $row['id'] : 0;
        $pipelineId = isset($row['pipeline_id']) ? (int) $row['pipeline_id'] : 0;
        $filename = isset($row['filename']) ? (string) $row['filename'] : '';
        $filepath = isset($row['filepath']) ? (string) $row['filepath'] : '';
        $mimeType = isset($row['mime_type']) ? (string) $row['mime_type'] : 'application/octet-stream';
        $fileSize = isset($row['file_size']) ? (int) $row['file_size'] : 0;
        $statusStr = isset($row['status']) ? (string) $row['status'] : 'pending';
        $errorMessage = isset($row['error_message']) ? (string) $row['error_message'] : null;
        $chunkCount = isset($row['chunk_count']) ? (int) $row['chunk_count'] : 0;
        $createdAtStr = isset($row['created_at']) ? (string) $row['created_at'] : 'now';
        $processedAtStr = isset($row['processed_at']) ? (string) $row['processed_at'] : null;

        return new self(
            id: $id,
            pipelineId: $pipelineId,
            filename: $filename,
            filepath: $filepath,
            mimeType: $mimeType,
            fileSize: $fileSize,
            status: DocumentStatus::from($statusStr),
            errorMessage: $errorMessage,
            chunkCount: $chunkCount,
            createdAt: new \DateTimeImmutable($createdAtStr),
            processedAt: $processedAtStr !== null ? new \DateTimeImmutable($processedAtStr) : null,
        );
    }

    /**
     * Check if document can be reprocessed.
     */
    public function canReprocess(): bool
    {
        return $this->status->canReprocess();
    }

    /**
     * Check if document is currently processing.
     */
    public function isProcessing(): bool
    {
        return $this->status->isActive();
    }

    /**
     * Get file size in human-readable format.
     */
    public function fileSizeFormatted(): string
    {
        $units = ['B', 'KB', 'MB', 'GB'];
        $size = (float) $this->fileSize;
        $unitIndex = 0;

        while ($size >= 1024 && $unitIndex < count($units) - 1) {
            $size /= 1024;
            $unitIndex++;
        }

        return round($size, 2) . ' ' . $units[$unitIndex];
    }
}
← Übersicht Graph