DrupalTempFileAdapter.php in Backup and Migrate 8.4        
                          
                  
                        
  
  
  
  
  
File
  src/File/DrupalTempFileAdapter.php
  
    View source  
  <?php
namespace BackupMigrate\Drupal\File;
use BackupMigrate\Core\File\TempFileAdapter;
use BackupMigrate\Core\File\TempFileAdapterInterface;
use Drupal\Core\File\FileSystemInterface;
class DrupalTempFileAdapter extends TempFileAdapter implements TempFileAdapterInterface {
  
  protected $filesystem;
  
  public function __construct(FileSystemInterface $filesystem, $dir = 'temporary://', $prefix = 'bam') {
    
    parent::__construct($dir, $prefix);
    $this->filesystem = $filesystem;
  }
  
  public function createTempFile($ext = '') {
    
    $ext = $ext ? '.' . $ext : '';
    $file = $this->filesystem
      ->tempnam($this->dir, $this->prefix);
    if (!$file) {
      throw new \Exception('Could not create a temporary file to write to.');
    }
    $this->tempfiles[] = $file;
    return $file;
  }
  
  public function deleteTempFile($filename) {
    
    if (in_array($filename, $this->tempfiles)) {
      if (file_exists($filename)) {
        if (!$this->filesystem
          ->unlink($filename)) {
          throw new \Exception('Could not delete a temporary file.');
        }
      }
    }
  }
}