public function __invoke(EditProjectCommand $command) { $project = $this->repository->projectOfId(ProjectId::generate($command->id())); if (!$project instanceof Project) { throw new ProjectDoesNotExistException(); } $organization = $this->organizationRepository->organizationOfId($project->organizationId()); if (!$organization->isOwner(UserId::generate($command->editorId()))) { throw new UnauthorizedProjectActionException(); } $project->edit(new ProjectName($command->name()), new Slug(null === $command->slug() ? $command->name() : $command->slug())); $this->repository->persist($project); }
function it_does_not_allow_to_edit_project_if_editor_is_not_organization_owner(OrganizationRepository $organizationRepository, ProjectRepository $projectRepository, EditProjectCommand $command, Project $project, Organization $organization, OrganizationId $organizationId) { $command->id()->willReturn('project-id'); $command->name()->willReturn('Project name'); $command->slug()->willReturn(null); $command->editorId()->willReturn('editor-id'); $projectRepository->projectOfId(ProjectId::generate('project-id'))->shouldBeCalled()->willReturn($project); $project->organizationId()->shouldBeCalled()->willReturn($organizationId); $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization); $organization->isOwner(UserId::generate('editor-id'))->shouldBeCalled()->willReturn(false); $this->shouldThrow(UnauthorizedProjectActionException::class)->during__invoke($command); }