refactorings

This commit is contained in:
Dr Masroor Ehsan 2025-01-12 01:20:23 +06:00
parent 24cbee5867
commit 2cfddb3ba2
2 changed files with 19 additions and 7 deletions

View File

@ -6,7 +6,6 @@
use App\Http\Controllers\HashidControllerBase; use App\Http\Controllers\HashidControllerBase;
use App\Http\Requests\StoreReportRequest; use App\Http\Requests\StoreReportRequest;
use App\Models\Study; use App\Models\Study;
use App\Models\StudyReport;
use App\Services\Report\ReportManager; use App\Services\Report\ReportManager;
class ReportController extends HashidControllerBase class ReportController extends HashidControllerBase
@ -63,8 +62,7 @@ public function create()
public function edit(string $uuid) public function edit(string $uuid)
{ {
ReportManager::ensureEditAccess(); ReportManager::ensureEditAccess();
$report = StudyReport::with(['study_id', 'id'])->accession($uuid)->firstOrFail(); $manager = ReportManager::fromReport($uuid);
$manager = ReportManager::make($report->study_id);
$view = $manager->check(); $view = $manager->check();
if ($view) { if ($view) {
@ -73,6 +71,7 @@ public function edit(string $uuid)
$manager->lockStudyIfRequired(); $manager->lockStudyIfRequired();
$study = $manager->getStudy(); $study = $manager->getStudy();
$report = $manager->getReport();
return view('staff.reports.create', compact('study', 'report')); return view('staff.reports.create', compact('study', 'report'));
} }
@ -80,8 +79,9 @@ public function edit(string $uuid)
public function view(string $uuid) public function view(string $uuid)
{ {
ReportManager::ensureDownloadAccess(); ReportManager::ensureDownloadAccess();
$report = StudyReport::with(['study_id', 'id'])->accession($uuid)->firstOrFail(); $manager = ReportManager::fromReport($uuid);
$title = 'View Report'; $title = 'View Report';
$report = $manager->getReport();
return view('staff.reports.viewer.html-report', compact('report', 'title')); return view('staff.reports.viewer.html-report', compact('report', 'title'));
} }

View File

@ -12,11 +12,11 @@
final class ReportManager 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 public function getReports(): Collection
@ -24,6 +24,13 @@ public function getReports(): Collection
return $this->study->reports->sortByDesc('created_at'); 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 public function createReport(string $content, ReportStatus $status): StudyReport
{ {
$report = StudyReport::make([ $report = StudyReport::make([
@ -126,4 +133,9 @@ public static function ensureEditAccess(): void
{ {
abort_unless(me()->may([Permission::ReportEdit, Permission::ReportDictate, Permission::ReportApprove]), 403); abort_unless(me()->may([Permission::ReportEdit, Permission::ReportDictate, Permission::ReportApprove]), 403);
} }
public function getReport(): ?StudyReport
{
return $this->report;
}
} }