You are here

class FlippingBook in Flipping Book 8

Same name in this branch
  1. 8 src/FlippingBook.php \Drupal\flipping_book\FlippingBook
  2. 8 src/Entity/FlippingBook.php \Drupal\flipping_book\Entity\FlippingBook

Class FlippingBook.

@package Drupal\flipping_book

Hierarchy

Expanded class hierarchy of FlippingBook

1 string reference to 'FlippingBook'
flipping_book.services.yml in ./flipping_book.services.yml
flipping_book.services.yml
1 service uses FlippingBook
flipping_book in ./flipping_book.services.yml
Drupal\flipping_book\FlippingBook

File

src/FlippingBook.php, line 17

Namespace

Drupal\flipping_book
View source
class FlippingBook implements FlippingBookInterface {

  /**
   * File System service.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * Archiver Manager service.
   *
   * @var \Drupal\Core\Archiver\ArchiverManager
   */
  protected $pluginManagerArchiver;

  /**
   * Entity Type Manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * FlippingBook constructor.
   *
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   A File System service instance.
   * @param \Drupal\Core\Archiver\ArchiverManager $plugin_manager_archiver
   *   An Archiver Manager service instance.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The Entity Type Manager service instance.
   */
  public function __construct(FileSystemInterface $file_system, ArchiverManager $plugin_manager_archiver, EntityTypeManagerInterface $entity_type_manager) {
    $this->fileSystem = $file_system;
    $this->pluginManagerArchiver = $plugin_manager_archiver;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function extractUploadLocation(FlippingBookEntity $flippingBook) {
    $flipping_book_type = $this
      ->getBundleEntity($flippingBook);
    return $flipping_book_type
      ->get('location');
  }

  /**
   * {@inheritdoc}
   */
  public function getBundleEntity(FlippingBookEntity $flippingBook) {
    $storage = $this->entityTypeManager
      ->getStorage('flipping_book_type');
    return $storage
      ->load($flippingBook
      ->bundle());
  }

  /**
   * {@inheritdoc}
   */
  public function prepareExportDirectory(File $file, $export_location) {
    $filepath = $this->fileSystem
      ->realpath($file
      ->getFileUri());
    $dir = pathinfo($this
      ->cleanFilename($file
      ->getFilename()), PATHINFO_FILENAME);
    $this->fileSystem
      ->prepareDirectory($export_location, FileSystemInterface::CREATE_DIRECTORY);
    $destination = $this->fileSystem
      ->getDestinationFilename($export_location . '/' . $dir, FileSystemInterface::EXISTS_RENAME);
    return [
      'filepath' => $filepath,
      'destination' => $destination,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function deleteArchive(FlippingBookEntity $flippingBook) {
    $dir = $flippingBook
      ->get('directory')->value;
    if (empty($dir)) {
      return;
    }
    $location = $this
      ->extractUploadLocation($flippingBook);
    $this->fileSystem
      ->deleteRecursive($this->fileSystem
      ->realpath("{$location}/{$dir}"));
  }

  /**
   * {@inheritdoc}
   */
  public function extractArchive($file_path, $destination) {
    $archiver = $this->pluginManagerArchiver
      ->getInstance([
      'filepath' => $file_path,
    ]);
    $archiver
      ->extract($this->fileSystem
      ->realpath($destination . '/'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildFlippingBookUrl(FlippingBookEntity $flippingBook) {
    $location = $this
      ->extractUploadLocation($flippingBook);
    $dir = $flippingBook
      ->getDirectory();
    $uri = file_create_url("{$location}/{$dir}/index.html");
    return Url::fromUri($uri);
  }

  /**
   * {@inheritdoc}
   */
  public function cleanFilename($filename) {

    // Replace whitespace.
    $filename = str_replace(' ', '_', $filename);

    // Remove remaining unsafe characters.
    $filename = preg_replace('![^0-9A-Za-z_.-]!', '', $filename);

    // Remove multiple consecutive non-alphabetical characters.
    $filename = preg_replace('/(_)_+|(\\.)\\.+|(-)-+/', '\\1\\2\\3', $filename);

    // Force lowercase to prevent issues on case-insensitive file systems.
    $filename = strtolower($filename);
    return $filename;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlippingBook::$entityTypeManager protected property Entity Type Manager service.
FlippingBook::$fileSystem protected property File System service.
FlippingBook::$pluginManagerArchiver protected property Archiver Manager service.
FlippingBook::buildFlippingBookUrl public function Build Flipping Book URL. Overrides FlippingBookInterface::buildFlippingBookUrl
FlippingBook::cleanFilename public function Helper function to sanitize flipping_book filename. Overrides FlippingBookInterface::cleanFilename
FlippingBook::deleteArchive public function Delete Flipping Book Archive. Overrides FlippingBookInterface::deleteArchive
FlippingBook::extractArchive public function Extract uploaded archive. Overrides FlippingBookInterface::extractArchive
FlippingBook::extractUploadLocation public function Extract upload location. Overrides FlippingBookInterface::extractUploadLocation
FlippingBook::getBundleEntity public function Get Bundle Entity. Overrides FlippingBookInterface::getBundleEntity
FlippingBook::prepareExportDirectory public function Prepare export directory. Overrides FlippingBookInterface::prepareExportDirectory
FlippingBook::__construct public function FlippingBook constructor.
FlippingBookInterface::FLIPPING_BOOK_PRIVATE constant Private Flipping Book stream definition.
FlippingBookInterface::FLIPPING_BOOK_PUBLIC constant Public Flipping Book stream definition.