You are here

class GetAlerts in Site Alert 8

Service for retrieving site alerts.

Hierarchy

Expanded class hierarchy of GetAlerts

1 string reference to 'GetAlerts'
site_alert.services.yml in ./site_alert.services.yml
site_alert.services.yml
1 service uses GetAlerts
site_alert.get_alerts in ./site_alert.services.yml
Drupal\site_alert\GetAlerts

File

src/GetAlerts.php, line 13

Namespace

Drupal\site_alert
View source
class GetAlerts implements GetAlertsInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new GetAlerts service.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  public function getActiveAlertIds() {
    $now = $this
      ->dateNow();
    $query = $this
      ->getStorage()
      ->getQuery();
    $start_value = $query
      ->orConditionGroup()
      ->condition('scheduling.value', $now, '<=')
      ->notExists('scheduling.value');
    $end_value = $query
      ->orConditionGroup()
      ->condition('scheduling.end_value', $now, '>')
      ->notExists('scheduling.end_value');
    $query
      ->condition('active', 1, '=')
      ->condition($start_value)
      ->condition($end_value);
    return $query
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function getActiveAlerts() {
    $ids = $this
      ->getActiveAlertIds();
    if (!empty($ids)) {
      return $this
        ->getStorage()
        ->loadMultiple($ids);
    }
    return [];
  }

  /**
   * Returns the date in correct timezone and format to compare with database.
   *
   * @return string
   *   The date string representing the current time.
   */
  protected function dateNow() {
    $now = new DrupalDateTime();
    $now
      ->setTimezone(new DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
    return $now
      ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
  }

  /**
   * Returns the entity storage for site alert entities.
   *
   * @return \Drupal\Core\Entity\EntityStorageInterface
   *   The entity storage.
   */
  protected function getStorage() {
    return $this->entityTypeManager
      ->getStorage('site_alert');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GetAlerts::$entityTypeManager protected property The entity type manager.
GetAlerts::dateNow protected function Returns the date in correct timezone and format to compare with database.
GetAlerts::getActiveAlertIds public function Returns the entity IDs of the currently active site alerts. Overrides GetAlertsInterface::getActiveAlertIds
GetAlerts::getActiveAlerts public function Returns the currently active site alerts. Overrides GetAlertsInterface::getActiveAlerts
GetAlerts::getStorage protected function Returns the entity storage for site alert entities.
GetAlerts::__construct public function Constructs a new GetAlerts service.