refactor(controllers): extract validation of created data into separate method
This commit is contained in:
parent
7b96b8dfb4
commit
97d23b3998
@ -99,20 +99,8 @@ public function estimate(string $countryCode, string $startDate, int $minutesDur
|
||||
return response()->json(['errors' => ['startDate' => ['validation.date_ymdhi']]], 400);
|
||||
}
|
||||
|
||||
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);
|
||||
if (($result = $this->validateParsedData($startDate, $startWorkday, $endWorkday)) !== null) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$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'),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user