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\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'));
}

View File

@ -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;
}
}