031e499819
API CI/CD / Validate (composer + pint) (push) Successful in 3m10s
API CI/CD / Test (PHPUnit) (push) Failing after 6m49s
API CI/CD / Build frontend assets (push) Successful in 1m3s
API CI/CD / Security audit (push) Failing after 1m0s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\SchoolYears;
|
|
|
|
use App\Models\SchoolYear;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use RuntimeException;
|
|
|
|
class SchoolYearWriteGuard
|
|
{
|
|
public function assertEditable(array $schoolYears): void
|
|
{
|
|
if (! Schema::hasTable('school_years')) {
|
|
return;
|
|
}
|
|
|
|
$years = array_values(array_unique(array_filter(array_map(
|
|
static fn ($year) => is_string($year) ? trim($year) : null,
|
|
$schoolYears
|
|
))));
|
|
|
|
if ($years === []) {
|
|
return;
|
|
}
|
|
|
|
$statuses = SchoolYear::query()
|
|
->whereIn('name', $years)
|
|
->get(['name', 'status'])
|
|
->keyBy('name');
|
|
|
|
foreach ($years as $year) {
|
|
$row = $statuses->get($year);
|
|
if ($row && in_array($row->status, [SchoolYear::STATUS_CLOSED, SchoolYear::STATUS_ARCHIVED], true)) {
|
|
throw new RuntimeException(sprintf(
|
|
'School year %s is %s and read-only.',
|
|
$year,
|
|
$row->status
|
|
));
|
|
}
|
|
}
|
|
}
|
|
}
|