|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\DTOs\DataProcessingJob; |
| 4 | + |
| 5 | +use App\Enums\DataProcessingJobStatus; |
| 6 | +use App\Enums\DataProcessingJobType; |
| 7 | +use App\Models\DataProcessingJob; |
| 8 | +use Illuminate\Http\Request; |
| 9 | + |
| 10 | +class DataProcessingJobDTO |
| 11 | +{ |
| 12 | + public function __construct( |
| 13 | + public readonly ?string $jobId, |
| 14 | + public readonly ?DataProcessingJobType $type, |
| 15 | + public readonly ?DataProcessingJobStatus $status, |
| 16 | + public readonly ?string $entityType, |
| 17 | + public readonly ?string $filePath, |
| 18 | + public readonly ?string $fileName, |
| 19 | + public readonly ?string $originalFileName, |
| 20 | + public readonly ?array $filters, |
| 21 | + public readonly ?array $errors, |
| 22 | + public readonly ?int $processedRows, |
| 23 | + public readonly ?int $successCount, |
| 24 | + public readonly ?int $errorCount, |
| 25 | + public readonly ?string $errorMessage, |
| 26 | + public readonly ?int $userId, |
| 27 | + public readonly ?\DateTime $startedAt, |
| 28 | + public readonly ?\DateTime $completedAt, |
| 29 | + ) {} |
| 30 | + |
| 31 | + public static function fromRequest(Request $request): self |
| 32 | + { |
| 33 | + return new self( |
| 34 | + $request->input('job_id'), |
| 35 | + $request->input('type') ? DataProcessingJobType::from($request->input('type')) : null, |
| 36 | + $request->input('status') ? DataProcessingJobStatus::from($request->input('status')) : null, |
| 37 | + $request->input('entity_type'), |
| 38 | + $request->input('file_path'), |
| 39 | + $request->input('file_name'), |
| 40 | + $request->input('original_file_name'), |
| 41 | + $request->input('filters') ? json_decode($request->input('filters'), true) : null, |
| 42 | + $request->input('errors') ? json_decode($request->input('errors'), true) : null, |
| 43 | + $request->input('processed_rows'), |
| 44 | + $request->input('success_count'), |
| 45 | + $request->input('error_count'), |
| 46 | + $request->input('error_message'), |
| 47 | + $request->input('user_id'), |
| 48 | + $request->input('started_at') ? new \DateTime($request->input('started_at')) : null, |
| 49 | + $request->input('completed_at') ? new \DateTime($request->input('completed_at')) : null, |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public static function fromArray(array $data): self |
| 54 | + { |
| 55 | + return new self( |
| 56 | + $data['job_id'] ?? null, |
| 57 | + isset($data['type']) ? ($data['type'] instanceof DataProcessingJobType ? $data['type'] : DataProcessingJobType::from($data['type'])) : null, |
| 58 | + isset($data['status']) ? ($data['status'] instanceof DataProcessingJobStatus ? $data['status'] : DataProcessingJobStatus::from($data['status'])) : null, |
| 59 | + $data['entity_type'] ?? null, |
| 60 | + $data['file_path'] ?? null, |
| 61 | + $data['file_name'] ?? null, |
| 62 | + $data['original_file_name'] ?? null, |
| 63 | + $data['filters'] ?? null, |
| 64 | + $data['errors'] ?? null, |
| 65 | + $data['processed_rows'] ?? null, |
| 66 | + $data['success_count'] ?? null, |
| 67 | + $data['error_count'] ?? null, |
| 68 | + $data['error_message'] ?? null, |
| 69 | + $data['user_id'] ?? null, |
| 70 | + isset($data['started_at']) ? ($data['started_at'] instanceof \DateTime ? $data['started_at'] : new \DateTime($data['started_at'])) : null, |
| 71 | + isset($data['completed_at']) ? ($data['completed_at'] instanceof \DateTime ? $data['completed_at'] : new \DateTime($data['completed_at'])) : null, |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + public static function fromModel(DataProcessingJob $model): self |
| 76 | + { |
| 77 | + return new self( |
| 78 | + $model->job_id, |
| 79 | + $model->type, |
| 80 | + $model->status, |
| 81 | + $model->entity_type, |
| 82 | + $model->file_path, |
| 83 | + $model->file_name, |
| 84 | + $model->original_file_name, |
| 85 | + $model->filters, |
| 86 | + $model->errors, |
| 87 | + $model->processed_rows, |
| 88 | + $model->success_count, |
| 89 | + $model->error_count, |
| 90 | + $model->error_message, |
| 91 | + $model->user_id, |
| 92 | + $model->started_at, |
| 93 | + $model->completed_at, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + public function toArray(): array |
| 98 | + { |
| 99 | + return array_filter([ |
| 100 | + 'job_id' => $this->jobId, |
| 101 | + 'type' => $this->type?->value, |
| 102 | + 'status' => $this->status?->value, |
| 103 | + 'entity_type' => $this->entityType, |
| 104 | + 'file_path' => $this->filePath, |
| 105 | + 'file_name' => $this->fileName, |
| 106 | + 'original_file_name' => $this->originalFileName, |
| 107 | + 'filters' => $this->filters, |
| 108 | + 'errors' => $this->errors, |
| 109 | + 'processed_rows' => $this->processedRows, |
| 110 | + 'success_count' => $this->successCount, |
| 111 | + 'error_count' => $this->errorCount, |
| 112 | + 'error_message' => $this->errorMessage, |
| 113 | + 'user_id' => $this->userId, |
| 114 | + 'started_at' => $this->startedAt?->format('Y-m-d H:i:s'), |
| 115 | + 'completed_at' => $this->completedAt?->format('Y-m-d H:i:s'), |
| 116 | + ], fn ($value) => ! is_null($value)); |
| 117 | + } |
| 118 | +} |
0 commit comments