public function __invoke(ChangeParentTaskCommand $command) { $task = $this->repository->taskOfId(TaskId::generate($command->id())); if (!$task instanceof Task) { throw new TaskDoesNotExistException(); } if (null !== ($parentId = $command->parentId())) { $parentId = TaskId::generate($parentId); if ($task->id()->equals($parentId)) { throw new TaskAndTaskParentCannotBeTheSameException(); } $parent = $this->repository->taskOfId($parentId); if (!$parent instanceof Task) { throw new TaskParentDoesNotExistException(); } if (!$task->projectId()->equals($parent->projectId())) { throw new TaskParentCannotBelongToOtherProjectException(); } } $project = $this->projectRepository->projectOfId($task->projectId()); $organization = $this->organizationRepository->organizationOfId($project->organizationId()); if (!$organization->isOrganizationMember(UserId::generate($command->changerId()))) { throw new UnauthorizedTaskActionException(); } $task->changeParent(!isset($parent) ? null : $parent->id()); $this->repository->persist($task); }
function it_does_not_change_parent_task_because_task_action_is_not_allowed(ChangeParentTaskCommand $command, TaskRepository $repository, Task $task, TaskId $taskId, Task $parent, ProjectId $projectId, ProjectRepository $projectRepository, Project $project, OrganizationRepository $organizationRepository, Organization $organization, OrganizationId $organizationId) { $command->id()->shouldBeCalled()->willReturn('task-id'); $repository->taskOfId(TaskId::generate('task-id'))->shouldBeCalled()->willReturn($task); $command->parentId()->shouldBeCalled()->willReturn('parent-id'); $task->id()->shouldBeCalled()->willReturn($taskId); $taskId->equals(TaskId::generate('parent-id'))->shouldBeCalled()->willReturn(false); $repository->taskOfId(TaskId::generate('parent-id'))->shouldBeCalled()->willReturn($parent); $task->projectId()->shouldBeCalled()->willReturn($projectId); $parent->projectId()->shouldBeCalled()->willReturn($projectId); $projectId->equals($projectId)->shouldBeCalled()->willReturn(true); $projectRepository->projectOfId($projectId)->shouldBeCalled()->willReturn($project); $project->organizationId()->shouldBeCalled()->willReturn($organizationId); $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization); $command->changerId()->shouldBeCalled()->willReturn('changer-id'); $organization->isOrganizationMember(UserId::generate('changer-id'))->shouldBeCalled()->willReturn(false); $this->shouldThrow(UnauthorizedTaskActionException::class)->during__invoke($command); }