You are here

EasyEmailStorage.php in Easy Email 2.0.x

Same filename and directory in other branches
  1. 8 src/EasyEmailStorage.php

Namespace

Drupal\easy_email

File

src/EasyEmailStorage.php
View source
<?php

namespace Drupal\easy_email;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\easy_email\Entity\EasyEmailInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 * Defines the storage handler class for Email entities.
 *
 * This extends the base storage class, adding required special handling for
 * Email entities.
 *
 * @ingroup easy_email
 */
class EasyEmailStorage extends SqlContentEntityStorage implements EasyEmailStorageInterface {

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * Constructs a new EasyEmailStorage object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection to be used.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache backend to be used.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface|null $memory_cache
   *   The memory cache backend to be used.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle info.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   */
  public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityFieldManagerInterface $entity_field_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache = NULL, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, EntityTypeManagerInterface $entity_type_manager = NULL, EventDispatcherInterface $event_dispatcher = NULL) {
    parent::__construct($entity_type, $database, $entity_field_manager, $cache, $language_manager, $memory_cache, $entity_type_bundle_info, $entity_type_manager);
    $this->eventDispatcher = $event_dispatcher;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('database'), $container
      ->get('entity_field.manager'), $container
      ->get('cache.entity'), $container
      ->get('language_manager'), $container
      ->get('entity.memory_cache'), $container
      ->get('entity_type.bundle.info'), $container
      ->get('entity_type.manager'), $container
      ->get('event_dispatcher'));
  }

  /**
   * @inheritDoc
   */
  public function getEmailTypeStorage() {
    return $this->entityTypeManager
      ->getStorage('easy_email_type');
  }

  /**
   * {@inheritdoc}
   */
  public function revisionIds(EasyEmailInterface $entity) {
    return $this->database
      ->query('SELECT vid FROM {easy_email_revision} WHERE id=:id ORDER BY vid', [
      ':id' => $entity
        ->id(),
    ])
      ->fetchCol();
  }

  /**
   * {@inheritdoc}
   */
  public function userRevisionIds(AccountInterface $account) {
    return $this->database
      ->query('SELECT vid FROM {easy_email_field_revision} WHERE uid = :uid ORDER BY vid', [
      ':uid' => $account
        ->id(),
    ])
      ->fetchCol();
  }

  /**
   * {@inheritdoc}
   */
  public function countDefaultLanguageRevisions(EasyEmailInterface $entity) {
    return $this->database
      ->query('SELECT COUNT(*) FROM {easy_email_field_revision} WHERE id = :id AND default_langcode = 1', [
      ':id' => $entity
        ->id(),
    ])
      ->fetchField();
  }

  /**
   * {@inheritdoc}
   */
  public function clearRevisionsLanguage(LanguageInterface $language) {
    return $this->database
      ->update('easy_email_revision')
      ->fields([
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ])
      ->condition('langcode', $language
      ->getId())
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  protected function invokeHook($hook, EntityInterface $entity) {
    parent::invokeHook($hook, $entity);
    $event_class = $this->entityType
      ->getHandlerClass('event');
    if ($event_class) {
      $this->eventDispatcher
        ->dispatch($this
        ->getEventName($hook), new $event_class($entity));
    }
  }

  /**
   * Gets the event name for the given hook.
   *
   * Created using the the entity type's module name and ID.
   *
   * @param string $hook
   *   One of 'load', 'create', 'presave', 'insert', 'update', 'predelete',
   *   'delete', 'translation_insert', 'translation_delete'.
   *
   * @return string
   *   The event name.
   */
  protected function getEventName($hook) {
    return $this->entityType
      ->getProvider() . '.' . $this->entityType
      ->id() . '.' . $hook;
  }

}

Classes

Namesort descending Description
EasyEmailStorage Defines the storage handler class for Email entities.