Модуль отчетов
This commit is contained in:
66
app/Services/Reports/ReportRegistry.php
Normal file
66
app/Services/Reports/ReportRegistry.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Reports;
|
||||
|
||||
use App\Models\ReportTemplate;
|
||||
use App\Models\User;
|
||||
use App\Services\Reports\BuiltIn\DutyDoctorReport;
|
||||
use App\Services\Reports\BuiltIn\HeadNurseReport;
|
||||
use App\Services\Reports\Contracts\ReportDefinition;
|
||||
|
||||
class ReportRegistry
|
||||
{
|
||||
public function __construct(private readonly ReportSourceRegistry $sources) {}
|
||||
|
||||
/** @return ReportDefinition[] */
|
||||
public function all(): array
|
||||
{
|
||||
$definitions = [
|
||||
new DutyDoctorReport($this->sources),
|
||||
new HeadNurseReport($this->sources),
|
||||
];
|
||||
|
||||
foreach (ReportTemplate::all() as $template) {
|
||||
$definitions[] = new TemplateReportDefinition($template, $this->sources);
|
||||
}
|
||||
|
||||
return $definitions;
|
||||
}
|
||||
|
||||
/** @return ReportDefinition[] */
|
||||
public function availableFor(User $user): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
$this->all(),
|
||||
fn (ReportDefinition $definition) => $this->isVisible($definition, $user)
|
||||
));
|
||||
}
|
||||
|
||||
public function find(string $code, User $user): ?ReportDefinition
|
||||
{
|
||||
foreach ($this->availableFor($user) as $definition) {
|
||||
if ($definition->code() === $code) {
|
||||
return $definition;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function isVisible(ReportDefinition $definition, User $user): bool
|
||||
{
|
||||
$permissions = $definition->requiredPermissions();
|
||||
|
||||
if (empty($permissions)) {
|
||||
return $user->currentRoleCan('report.view') || $user->currentRoleCan('nurse.report.view');
|
||||
}
|
||||
|
||||
foreach ($permissions as $permission) {
|
||||
if ($user->currentRoleCan($permission)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user