You are here

class BackupDatabaseFileHandler in Backup Database 8

BackupDatabaseFileHandler class.

Hierarchy

Expanded class hierarchy of BackupDatabaseFileHandler

1 file declares its use of BackupDatabaseFileHandler
BackupDatabaseClient.php in src/BackupDatabaseClient.php
Contains \Drupal\backup_db\BackupDatabaseClient
1 string reference to 'BackupDatabaseFileHandler'
backup_db.services.yml in ./backup_db.services.yml
backup_db.services.yml
1 service uses BackupDatabaseFileHandler
backup_db.handler in ./backup_db.services.yml
Drupal\backup_db\BackupDatabaseFileHandler

File

src/BackupDatabaseFileHandler.php, line 16
Contains \Drupal\backup_db\BackupDatabaseFileHandler

Namespace

Drupal\backup_db
View source
class BackupDatabaseFileHandler {

  /**
   * @var \Drupal\backup_db\BackupDatabaseFile
   */
  protected $file;

  /**
   * @var \Drupal\Core\File\FileSystem
   */
  protected $filesystem;

  /**
   * @var File config
   */
  protected $config;

  /**
   * @param \Drupal\backup_db\BackupDatabaseFile
   *    The database export file to act on.
   *
   * @param \Drupal\Core\File\FileSystem $filesystem
   *    Provides helpers to operate on files and stream wrappers.
   */
  public function __construct(BackupDatabaseFile $file, FileSystem $filesystem) {
    $this->file = $file;
    $this->filesystem = $filesystem;
  }

  /**
   * Handles file generation.
   *
   * @param $config
   */
  public function setupFile($config) {
    $this
      ->setConfig($config);
    $path = $this
      ->createFilePath();
    $extension = $this
      ->createFileType();
    $this->file
      ->setFileName($this->config['name'] . '_' . time() . $extension);
    $this->file
      ->setFileUri(file_create_filename($this->file
      ->getFileName(), $path));
  }

  /**
   * @inheritdoc
   */
  public function getFile() {
    return $this->file;
  }

  /**
   * @inheritdoc
   */
  public function setFile(BackupDatabaseFile $file) {
    $this->file = $file;
  }

  /**
   * @inheritdoc
   *
   * @todo,
   */
  public function exits($uri) {
  }

  /**
   * Set default file info.
   */
  private function setConfig($config) {
    $this->config = [
      'date' => \Drupal::service('date.formatter')
        ->format(time(), $config
        ->get('date')),
      'compress' => $config
        ->get('settings.compress'),
      'name' => $config
        ->get('filename'),
      'path' => $config
        ->get('path'),
    ];
  }

  /**
   * Create export location.
   */
  private function createFilePath() {
    $result = $this->config['path'];
    if (!file_prepare_directory($result, FILE_CREATE_DIRECTORY)) {
      $result = FALSE;
      \Drupal::logger('backup_db')
        ->error('The requested directory @dir could not be created.', array(
        '@dir' => $this->config['path'],
      ));
    }
    else {
      if (!file_prepare_directory($result)) {
        $result = FALSE;
        \Drupal::logger('backup_db')
          ->error('The requested directory @dir permissions are not writable.', array(
          '@dir' => $this->config['path'],
        ));
      }
    }
    if ($this->config['date']) {
      $filepath = $this->config['path'] . '/' . $this->config['date'];
      if (file_prepare_directory($filepath, FILE_CREATE_DIRECTORY)) {
        $result = $filepath;
      }
    }
    return $result;
  }

  /**
   * Determine the export type/ extension.
   */
  private function createFileType() {
    $type = 'application/octet-stream';
    $extension = '.sql';
    switch ($this->config['compress']) {
      case 'Gzip':
        $extension .= '.gz';
        $type = 'application/gzip';
        break;
      case 'Bzip2':
        $extension .= '.bz2';
        $type = 'application/x-bzip2';
    }
    $this->file
      ->setFileType($type);
    return $extension;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BackupDatabaseFileHandler::$config protected property
BackupDatabaseFileHandler::$file protected property
BackupDatabaseFileHandler::$filesystem protected property
BackupDatabaseFileHandler::createFilePath private function Create export location.
BackupDatabaseFileHandler::createFileType private function Determine the export type/ extension.
BackupDatabaseFileHandler::exits public function @inheritdoc
BackupDatabaseFileHandler::getFile public function @inheritdoc
BackupDatabaseFileHandler::setConfig private function Set default file info.
BackupDatabaseFileHandler::setFile public function @inheritdoc
BackupDatabaseFileHandler::setupFile public function Handles file generation.
BackupDatabaseFileHandler::__construct public function