extract validation code to private method
This commit is contained in:
parent
49663d70d9
commit
27a90c27c0
1 changed files with 31 additions and 24 deletions
|
|
@ -30,33 +30,10 @@ class CreatePlan
|
|||
*/
|
||||
public function execute(CreatePlanRequest $request): Plan
|
||||
{
|
||||
if ($request->userId === null) {
|
||||
throw new BadRequestException('userId is required');
|
||||
}
|
||||
|
||||
if ($request->textId === null) {
|
||||
throw new BadRequestException('textId is required');
|
||||
}
|
||||
|
||||
if ($request->name === null) {
|
||||
throw new BadRequestException('name is required');
|
||||
}
|
||||
|
||||
if ($request->dateStart === null) {
|
||||
throw new BadRequestException('date start is required');
|
||||
}
|
||||
|
||||
if ($request->dateEnd === null) {
|
||||
throw new BadRequestException('date end is required');
|
||||
}
|
||||
|
||||
$this->validate($request);
|
||||
$startDate = new DateTimeImmutable($request->dateStart);
|
||||
$endDate = new DateTimeImmutable($request->dateEnd);
|
||||
|
||||
if ($endDate < $startDate) {
|
||||
throw new BadRequestException('date end cannot be before date start');
|
||||
}
|
||||
|
||||
$userId = $request->userId;
|
||||
$user = $this->userRepo->find($userId);
|
||||
if ($user === null) {
|
||||
|
|
@ -115,4 +92,34 @@ class CreatePlan
|
|||
}
|
||||
return array_values($result);
|
||||
}
|
||||
|
||||
private function validate(CreatePlanRequest $request): void
|
||||
{
|
||||
if ($request->userId === null) {
|
||||
throw new BadRequestException('userId is required');
|
||||
}
|
||||
|
||||
if ($request->textId === null) {
|
||||
throw new BadRequestException('textId is required');
|
||||
}
|
||||
|
||||
if ($request->name === null) {
|
||||
throw new BadRequestException('name is required');
|
||||
}
|
||||
|
||||
if ($request->dateStart === null) {
|
||||
throw new BadRequestException('date start is required');
|
||||
}
|
||||
|
||||
if ($request->dateEnd === null) {
|
||||
throw new BadRequestException('date end is required');
|
||||
}
|
||||
|
||||
$startDate = new DateTimeImmutable($request->dateStart);
|
||||
$endDate = new DateTimeImmutable($request->dateEnd);
|
||||
|
||||
if ($endDate < $startDate) {
|
||||
throw new BadRequestException('date end cannot be before date start');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue