function it_does_not_handle_because_file_already_exists(FileRepository $repository, Filesystem $filesystem, UploadFileCommand $command) { $command->id()->shouldBeCalled()->willReturn('file-id'); $id = new FileId('file-id'); $command->name()->shouldBeCalled()->willReturn('dummy-file-name.pdf'); $name = new FileName('dummy-file-name.pdf'); $repository->fileOfId($id)->shouldBeCalled()->willReturn(null); $filesystem->has($name)->shouldBeCalled()->willReturn(true); $this->shouldThrow(FileAlreadyExistsException::class)->during__invoke($command); }
/** * Handles the given command. * * @param UploadFileCommand $aCommand The command * * @throws FileAlreadyExistsException when file is already exists */ public function __invoke(UploadFileCommand $aCommand) { $id = new FileId($aCommand->id()); $file = $this->repository->fileOfId($id); if (null !== $file) { throw new FileAlreadyExistsException(); } $name = new FileName($aCommand->name()); if (true === $this->filesystem->has($name)) { throw new FileAlreadyExistsException(); } $this->filesystem->write($name, $aCommand->uploadedFile()); $file = $this->factory->build($id, $name, new FileMimeType($aCommand->mimeType())); $this->repository->persist($file); }