public function __invoke(CreateProjectCommand $command) { $project = $this->projectRepository->projectOfId(ProjectId::generate($command->id())); if ($project instanceof Project) { throw new ProjectAlreadyExists($project->id()); } $organizationId = OrganizationId::generate($command->organizationId()); $organization = $this->organizationRepository->organizationOfId($organizationId); if (!$organization instanceof Organization) { throw new OrganizationDoesNotExistException(); } if (!$organization->isOwner(UserId::generate($command->creatorId()))) { throw new UnauthorizedCreateProjectException(); } $project = new Project(ProjectId::generate($command->id()), new ProjectName($command->name()), new Slug(null === $command->slug() ? $command->name() : $command->slug()), $organization->id()); $this->projectRepository->persist($project); }
function it_creates_project_with_default_slug(OrganizationRepository $organizationRepository, ProjectRepository $projectRepository, CreateProjectCommand $command, Organization $organization, OrganizationId $organizationId) { $command->id()->willReturn('project-id'); $command->name()->willReturn('Project name'); $command->slug()->willReturn(null); $command->organizationId()->willReturn('organization-id'); $command->creatorId()->willReturn('creator-id'); $projectRepository->projectOfId(Argument::type(ProjectId::class))->shouldBeCalled()->willReturn(null); $organizationRepository->organizationOfId(Argument::type(OrganizationId::class))->shouldBeCalled()->willReturn($organization); $organization->isOwner(UserId::generate('creator-id'))->shouldBeCalled()->willReturn(true); $organization->id()->shouldBeCalled()->willReturn($organizationId); $projectRepository->persist(Argument::type(Project::class))->shouldBeCalled(); $this->__invoke($command); }