refactor(controllers): extract validation of created data into separate method

This commit is contained in:
Ondrej Vlach 2024-08-07 14:18:55 +02:00
parent 7b96b8dfb4
commit 97d23b3998
No known key found for this signature in database
GPG Key ID: 7F141CDACEDEE2DE

View File

@ -99,20 +99,8 @@ public function estimate(string $countryCode, string $startDate, int $minutesDur
return response()->json(['errors' => ['startDate' => ['validation.date_ymdhi']]], 400); return response()->json(['errors' => ['startDate' => ['validation.date_ymdhi']]], 400);
} }
if ($startWorkday->isAfter($endWorkday)) { if (($result = $this->validateParsedData($startDate, $startWorkday, $endWorkday)) !== null) {
return response()->json(['errors' => ['startWorkday' => ['validation.start_workday_must_be_before_end_workday']]], 400); return $result;
}
if ($startWorkday->isEqualTo($endWorkday)) {
return response()->json(['errors' => ['startWorkday' => ['validation.start_workday_must_not_be_equal_to_end_workday']]], 400);
}
if ($startWorkday->isAfter(LocalTime::fromNativeDateTime($startDate))) {
return response()->json(['errors' => ['startDate' => ['validation.start_workday_must_be_before_start_date']]], 400);
}
if ($endWorkday->isBefore(LocalTime::fromNativeDateTime($startDate))) {
return response()->json(['errors' => ['endWorkday' => ['validation.end_workday_must_be_after_start_date']]], 400);
} }
$workMinutes = $this->durationConvertor->convertIntoWorkDuration($duration, $startWorkday, $endWorkday, $startDate); $workMinutes = $this->durationConvertor->convertIntoWorkDuration($duration, $startWorkday, $endWorkday, $startDate);
@ -133,4 +121,26 @@ public function estimate(string $countryCode, string $startDate, int $minutesDur
'estimation_date' => $dueDate->format('Y-m-d H:i:s'), 'estimation_date' => $dueDate->format('Y-m-d H:i:s'),
]); ]);
} }
private function validateParsedData(\DateTimeInterface $startDate, LocalTime $startWorkday, LocalTime $endWorkday): JsonResponse|null
{
if ($startWorkday->isAfter($endWorkday)) {
return response()->json(['errors' => ['startWorkday' => ['validation.start_workday_must_be_before_end_workday']]], 400);
}
if ($startWorkday->isEqualTo($endWorkday)) {
return response()->json(['errors' => ['startWorkday' => ['validation.start_workday_must_not_be_equal_to_end_workday']]], 400);
}
if ($startWorkday->isAfter(LocalTime::fromNativeDateTime($startDate))) {
return response()->json(['errors' => ['startDate' => ['validation.start_workday_must_be_before_start_date']]], 400);
}
if ($endWorkday->isBefore(LocalTime::fromNativeDateTime($startDate))) {
return response()->json(['errors' => ['endWorkday' => ['validation.end_workday_must_be_after_start_date']]], 400);
}
return null;
}
} }