35 lines
765 B
PHP
35 lines
765 B
PHP
<?php
|
|
|
|
namespace App\Plan\UseCases;
|
|
|
|
use App\Plan\CreatePlanDto;
|
|
use App\Plan\Plan;
|
|
use App\Plan\PlanRepository;
|
|
use App\User\UserRepository;
|
|
use DomainException;
|
|
|
|
class CreatePlan
|
|
{
|
|
public function __construct(
|
|
private PlanRepository $planRepo,
|
|
private UserRepository $userRepo,
|
|
) {}
|
|
|
|
/**
|
|
* @throws DomainException
|
|
*/
|
|
public function execute(CreatePlanRequest $request): Plan
|
|
{
|
|
$userId = $request->userId;
|
|
$user = $this->userRepo->find($userId);
|
|
|
|
if ($user === null) {
|
|
throw new DomainException("User with id: $userId doesnt exist");
|
|
}
|
|
|
|
return $this->planRepo->create(new CreatePlanDto(
|
|
name: $request->name,
|
|
user: $user,
|
|
));
|
|
}
|
|
}
|