/** * @param \Dvs\FileBundle\Storage\Naming\NamingStrategy $namingStrategy * @param \Dvs\FileBundle\Storage\PathGenerating\FilePathStrategy $filePathStrategy * @param \Dvs\FileBundle\Interfaces\FileSystem $filesystem * @param \Dvs\FileBundle\Storage\Resource\Storageable $storageable */ public function it_should_throw_an_exception_if_resource_can_not_be_stored($namingStrategy, $filePathStrategy, $filesystem, $storageable) { // Given $fileInfo = new \SplFileInfo(__FILE__); $namingStrategy->generate($storageable)->willReturn($filename = 'filename'); $filePathStrategy->generate($filename)->willReturn($path = 'path/to/file/filename'); $fileExtension = $fileInfo->getExtension(); $filename .= '.' . $fileExtension; $filePathWithName = $path . DIRECTORY_SEPARATOR . $filename; $filesystem->write($filePathWithName, file_get_contents($fileInfo->getPathname()))->willReturn(false); // When $this->shouldThrow(FileStoreException::notStored())->duringStore($storageable, $fileInfo); // Then }
/** * @param Storageable $storageable * @param \SplFileInfo $fileInfo * * @return bool */ public function store(Storageable $storageable, \SplFileInfo $fileInfo) : bool { $fileName = $this->namingStrategy->generate($storageable); $filePath = $this->filePathStrategy->generate($fileName); $fileExtension = $this->getFileExtension($fileInfo); if ($fileExtension) { $fileName .= '.' . $fileExtension; } $storageable->setFileName($fileName); $filePathWithName = $filePath . DIRECTORY_SEPARATOR . $fileName; if (!$this->filesystem->write($filePathWithName, file_get_contents($fileInfo->getPathname()))) { throw FileStoreException::notStored(); } return true; }