You are here

abstract class RecipientHandlerBase in Simplenews 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/simplenews/RecipientHandler/RecipientHandlerBase.php \Drupal\simplenews\Plugin\simplenews\RecipientHandler\RecipientHandlerBase
  2. 8 src/Plugin/simplenews/RecipientHandler/RecipientHandlerBase.php \Drupal\simplenews\Plugin\simplenews\RecipientHandler\RecipientHandlerBase

Base class for all Recipient Handler classes.

Hierarchy

Expanded class hierarchy of RecipientHandlerBase

1 file declares its use of RecipientHandlerBase
RecipientHandlerSiteMail.php in modules/simplenews_demo/src/Plugin/simplenews/RecipientHandler/RecipientHandlerSiteMail.php

File

src/Plugin/simplenews/RecipientHandler/RecipientHandlerBase.php, line 12

Namespace

Drupal\simplenews\Plugin\simplenews\RecipientHandler
View source
abstract class RecipientHandlerBase extends PluginBase implements RecipientHandlerInterface {

  /**
   * The newsletter issue.
   *
   * @var \Drupal\Core\Entity\ContentEntityInterface
   */
  protected $issue;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The newsletter IDs.
   *
   * @var array
   */
  protected $newsletterIds;

  /**
   * RecipientHandlerBase constructor.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->issue = $configuration['_issue'];
    $this->connection = $configuration['_connection'];
    $this->newsletterIds = $configuration['_newsletter_ids'];
  }

  /**
   * {@inheritdoc}
   */
  public function count() {
    $cache =& drupal_static(__METHOD__, []);
    $cid = $this->pluginId . ':' . implode(':', $this->newsletterIds);
    if (isset($cache[$cid])) {
      return $cache[$cid];
    }
    $count = $this
      ->doCount();
    if ($this
      ->cacheCount()) {
      $cache[$cid] = $count;
    }
    return $count;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm() {
    return [];
  }

  /**
   * Counts the number of recipients.
   *
   * Internal count function allowing the caller to perform caching.
   *
   * @return int
   *   Number of recipients.
   */
  protected abstract function doCount();

  /**
   * Checks if the recipient count can be cached.
   *
   * Caching is allowed if the count depends only on the newsletter IDs, and
   * does not vary with a specific issue or handler settings.
   *
   * @return bool
   *   TRUE if the count can be cached.
   */
  protected function cacheCount() {
    return FALSE;
  }

  /**
   * Returns the newsletter ID.
   *
   * @return int
   *   Newsletter ID.
   *
   * @throws \Exception
   *   The configuration doesn't specify a single newsletter ID.
   */
  protected function getNewsletterId() {
    if (count($this->newsletterIds) != 1) {
      throw new \Exception("Recipient handler requires a single newsletter ID.");
    }
    return $this->newsletterIds[0];
  }

  /**
   * Adds an array of entries to the spool.
   *
   * The caller specifies the values for a field to define the recipient.  The
   * other fields are automatically defaulted based on the issue and
   * newsletter.
   *
   * @param string $field
   *   Field to set: 'snid', 'data' (automatically serialised) or 'uid'
   *   (automatically stored in 'data' array with key 'uid').
   * @param array $values
   *   Values to set for field.
   */
  protected function addArrayToSpool($field, array $values) {
    if (empty($values)) {
      return;
    }
    $template = [
      'entity_type' => $this->issue
        ->getEntityTypeId(),
      'entity_id' => $this->issue
        ->id(),
      'status' => SpoolStorageInterface::STATUS_PENDING,
      'timestamp' => REQUEST_TIME,
      'newsletter_id' => $this
        ->getNewsletterId(),
    ];
    if ($field == 'uid') {
      $field = 'data';
      $values = array_map(function ($v) {
        return [
          'uid' => $v,
        ];
      }, $values);
    }
    $insert = $this->connection
      ->insert('simplenews_mail_spool')
      ->fields(array_merge(array_keys($template), [
      $field,
    ]));
    foreach ($values as $value) {
      $row = $template;
      $row[$field] = $field == 'data' ? serialize($value) : $value;
      $insert
        ->values($row);
    }
    $insert
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
RecipientHandlerBase::$connection protected property The database connection.
RecipientHandlerBase::$issue protected property The newsletter issue.
RecipientHandlerBase::$newsletterIds protected property The newsletter IDs.
RecipientHandlerBase::addArrayToSpool protected function Adds an array of entries to the spool.
RecipientHandlerBase::cacheCount protected function Checks if the recipient count can be cached. 2
RecipientHandlerBase::count public function
RecipientHandlerBase::doCount abstract protected function Counts the number of recipients. 3
RecipientHandlerBase::getNewsletterId protected function Returns the newsletter ID.
RecipientHandlerBase::settingsForm public function Returns the elements to add to the settings form for handler settings. Overrides RecipientHandlerInterface::settingsForm 1
RecipientHandlerBase::__construct public function RecipientHandlerBase constructor. Overrides PluginBase::__construct
RecipientHandlerInterface::addToSpool public function Adds a newsletter issue to the mail spool. 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.