/** * 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); }
function it_handles(ByHashUploadFileCommand $command, Filesystem $filesystem, FileRepository $repository, FileFactory $factory, File $file) { $command->id()->shouldBeCalled()->willReturn('file-id'); $command->name()->shouldBeCalled()->willReturn('dummy-file-name.pdf'); $command->uploadedFile()->shouldBeCalled()->willReturn('test-content'); $command->mimeType()->shouldBeCalled()->willReturn('application/pdf'); $id = new FileId('file-id'); $mimeType = new FileMimeType('application/pdf'); $repository->fileOfId($id)->shouldBeCalled()->willReturn(null); $filesystem->has(Argument::type(FileName::class))->shouldBeCalled()->willReturn(false); $filesystem->write(Argument::type(FileName::class), 'test-content')->shouldBeCalled(); $factory->build($id, Argument::type(FileName::class), $mimeType)->shouldBeCalled()->willReturn($file); $repository->persist($file)->shouldBeCalled(); $this->__invoke($command); }