function it_does_not_handle_because_file_already_exists(FileRepository $repository, Filesystem $filesystem, ByHashUploadFileCommand $command) { $command->id()->shouldBeCalled()->willReturn('file-id'); $id = new FileId('file-id'); $command->name()->shouldBeCalled()->willReturn('dummy-file-name.pdf'); $repository->fileOfId($id)->shouldBeCalled()->willReturn(null); $filesystem->has(Argument::type(FileName::class))->shouldBeCalled()->willReturn(true); $this->shouldThrow(FileAlreadyExistsException::class)->during__invoke($command); }
/** * Handles the given command. * * @param ByHashUploadFileCommand $aCommand The command * * @throws FileAlreadyExistsException when file is already exists */ public function __invoke(ByHashUploadFileCommand $aCommand) { $id = new FileId($aCommand->id()); $file = $this->repository->fileOfId($id); if (null !== $file) { throw new FileAlreadyExistsException(); } $name = FileName::fromHash($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); }