This commit is contained in:
@@ -22,7 +22,7 @@ final class SchoolYearContextService
|
||||
IncomingRequest $request,
|
||||
?int $routeSchoolYearId = null
|
||||
): SchoolYearContext {
|
||||
$requestedId = $routeSchoolYearId ?? $this->normalizeInt($request->getGet('school_year_id'));
|
||||
$requestedId = $routeSchoolYearId ?? $this->requestedSchoolYearId($request);
|
||||
$requestedName = $this->legacyYearName($request);
|
||||
$userId = (int) (session()->get('user_id') ?? 0);
|
||||
|
||||
@@ -118,6 +118,27 @@ final class SchoolYearContextService
|
||||
return $context;
|
||||
}
|
||||
|
||||
public function forYearName(string $yearName): SchoolYearContext
|
||||
{
|
||||
$yearName = trim($yearName);
|
||||
|
||||
if ($yearName === '') {
|
||||
throw new SchoolYearNotFoundException('Selected school year was not found.');
|
||||
}
|
||||
|
||||
$row = $this->schoolYearModel
|
||||
->where('name', $yearName)
|
||||
->first();
|
||||
|
||||
if ($row === null) {
|
||||
throw new SchoolYearNotFoundException('Selected school year was not found.');
|
||||
}
|
||||
|
||||
$userId = (int) (session()->get('user_id') ?? 0);
|
||||
|
||||
return $this->authorizedContext($row, $userId, true);
|
||||
}
|
||||
|
||||
public function clearSelection(): void
|
||||
{
|
||||
session()->remove('selected_school_year_id');
|
||||
@@ -155,6 +176,31 @@ final class SchoolYearContextService
|
||||
return (int) $value;
|
||||
}
|
||||
|
||||
private function requestedSchoolYearId(IncomingRequest $request): ?int
|
||||
{
|
||||
$ids = [];
|
||||
|
||||
foreach (['school_year_id', 'schoolYearId', 'year_id'] as $key) {
|
||||
$value = $this->normalizeInt($request->getGet($key));
|
||||
if ($value !== null) {
|
||||
$ids[$key] = $value;
|
||||
}
|
||||
|
||||
if ($this->isWriteRequest($request)) {
|
||||
$value = $this->normalizeInt($request->getPost($key));
|
||||
if ($value !== null) {
|
||||
$ids['post:' . $key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count(array_unique($ids)) > 1) {
|
||||
throw new InvalidSchoolYearSelectionException('Conflicting school-year parameters were provided.');
|
||||
}
|
||||
|
||||
return $ids === [] ? null : (int) reset($ids);
|
||||
}
|
||||
|
||||
private function legacyYearName(IncomingRequest $request): string
|
||||
{
|
||||
$names = [];
|
||||
@@ -164,6 +210,13 @@ final class SchoolYearContextService
|
||||
if ($value !== '') {
|
||||
$names[$key] = $value;
|
||||
}
|
||||
|
||||
if ($this->isWriteRequest($request)) {
|
||||
$value = trim((string) ($request->getPost($key) ?? ''));
|
||||
if ($value !== '') {
|
||||
$names['post:' . $key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count(array_unique($names)) > 1) {
|
||||
@@ -179,6 +232,11 @@ final class SchoolYearContextService
|
||||
return $names === [] ? '' : (string) reset($names);
|
||||
}
|
||||
|
||||
private function isWriteRequest(IncomingRequest $request): bool
|
||||
{
|
||||
return in_array(strtoupper($request->getMethod()), ['POST', 'PUT', 'PATCH', 'DELETE'], true);
|
||||
}
|
||||
|
||||
private function authorizedContext(array $row, int $userId, bool $explicit): SchoolYearContext
|
||||
{
|
||||
if (! $this->isSelectable($row, $userId)) {
|
||||
|
||||
Reference in New Issue
Block a user