66 lines
1.7 KiB
PHP
66 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\ScheduledNode\UseCases;
|
|
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Node\NodeRepository;
|
|
use App\Plan\PlanRepository;
|
|
use App\ScheduledNode\ScheduledNode;
|
|
use App\ScheduledNode\CreateScheduledNodeDto;
|
|
use App\ScheduledNode\ScheduledNodeRepository;
|
|
use DateTimeImmutable;
|
|
use DomainException;
|
|
|
|
class CreateScheduledNode
|
|
{
|
|
public function __construct(
|
|
private ScheduledNodeRepository $scheduledNodeRepo,
|
|
private PlanRepository $planRepo,
|
|
private NodeRepository $nodeRepo,
|
|
) {}
|
|
|
|
/**
|
|
* @throws BadRequestException
|
|
* @throws DomainException
|
|
*/
|
|
public function execute(
|
|
CreateScheduledNodeRequest $request
|
|
): ScheduledNode {
|
|
$nodeId = $request->nodeId;
|
|
$planId = $request->planId;
|
|
$date = $request->date;
|
|
if ($date === null) {
|
|
throw new BadRequestException('date is required');
|
|
}
|
|
|
|
if ($planId === null) {
|
|
throw new BadRequestException('planId is required');
|
|
}
|
|
|
|
if ($nodeId === null) {
|
|
throw new BadRequestException('nodeId is required');
|
|
}
|
|
|
|
$plan = $this->planRepo->find($planId);
|
|
if ($plan === null) {
|
|
throw new DomainException(
|
|
"Plan with id: $planId doesnt exist"
|
|
);
|
|
}
|
|
|
|
$node = $this->nodeRepo->find($nodeId);
|
|
if ($node === null) {
|
|
throw new DomainException(
|
|
"Node with id: $nodeId doesnt exist"
|
|
);
|
|
}
|
|
|
|
return $this->scheduledNodeRepo->create(
|
|
new CreateScheduledNodeDto(
|
|
date: new DateTimeImmutable($date),
|
|
plan: $plan,
|
|
node: $node,
|
|
)
|
|
);
|
|
}
|
|
}
|