From 2cfddb3ba2477faa17913fc94700e3ef19255bc5 Mon Sep 17 00:00:00 2001 From: Dr Masroor Ehsan Date: Sun, 12 Jan 2025 01:20:23 +0600 Subject: [PATCH] refactorings --- .../Controllers/Staff/ReportController.php | 8 ++++---- app/Services/Report/ReportManager.php | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Staff/ReportController.php b/app/Http/Controllers/Staff/ReportController.php index a204e08..7eb5c1f 100644 --- a/app/Http/Controllers/Staff/ReportController.php +++ b/app/Http/Controllers/Staff/ReportController.php @@ -6,7 +6,6 @@ use App\Http\Controllers\HashidControllerBase; use App\Http\Requests\StoreReportRequest; use App\Models\Study; -use App\Models\StudyReport; use App\Services\Report\ReportManager; class ReportController extends HashidControllerBase @@ -63,8 +62,7 @@ public function create() public function edit(string $uuid) { ReportManager::ensureEditAccess(); - $report = StudyReport::with(['study_id', 'id'])->accession($uuid)->firstOrFail(); - $manager = ReportManager::make($report->study_id); + $manager = ReportManager::fromReport($uuid); $view = $manager->check(); if ($view) { @@ -73,6 +71,7 @@ public function edit(string $uuid) $manager->lockStudyIfRequired(); $study = $manager->getStudy(); + $report = $manager->getReport(); return view('staff.reports.create', compact('study', 'report')); } @@ -80,8 +79,9 @@ public function edit(string $uuid) public function view(string $uuid) { ReportManager::ensureDownloadAccess(); - $report = StudyReport::with(['study_id', 'id'])->accession($uuid)->firstOrFail(); + $manager = ReportManager::fromReport($uuid); $title = 'View Report'; + $report = $manager->getReport(); return view('staff.reports.viewer.html-report', compact('report', 'title')); } diff --git a/app/Services/Report/ReportManager.php b/app/Services/Report/ReportManager.php index 900e123..18c8658 100644 --- a/app/Services/Report/ReportManager.php +++ b/app/Services/Report/ReportManager.php @@ -12,11 +12,11 @@ final class ReportManager { - public function __construct(private readonly Study $study) {} + public function __construct(private readonly Study $study, private ?StudyReport $report = null) {} - public static function make(int $study_id): static + public static function make(int $study_id, ?StudyReport $report = null): static { - return new self(Study::findOrFail($study_id)); + return new self(Study::findOrFail($study_id), $report); } public function getReports(): Collection @@ -24,6 +24,13 @@ public function getReports(): Collection return $this->study->reports->sortByDesc('created_at'); } + public static function fromReport(string $uuid): static + { + $report = StudyReport::accession($uuid)->select(['id', 'study_id', 'file_path', 'created_at'])->firstOrFail(); + + return static::make($report->study_id, $report); + } + public function createReport(string $content, ReportStatus $status): StudyReport { $report = StudyReport::make([ @@ -126,4 +133,9 @@ public static function ensureEditAccess(): void { abort_unless(me()->may([Permission::ReportEdit, Permission::ReportDictate, Permission::ReportApprove]), 403); } + + public function getReport(): ?StudyReport + { + return $this->report; + } }