You are here

class ActiveHours in SMS Framework 8

Same name and namespace in other branches
  1. 2.x modules/sms_user/src/ActiveHours.php \Drupal\sms_user\ActiveHours
  2. 2.1.x modules/sms_user/src/ActiveHours.php \Drupal\sms_user\ActiveHours

Defines the user active hours service.

Hierarchy

Expanded class hierarchy of ActiveHours

1 string reference to 'ActiveHours'
sms_user.services.yml in modules/sms_user/sms_user.services.yml
modules/sms_user/sms_user.services.yml
1 service uses ActiveHours
sms_user.active_hours in modules/sms_user/sms_user.services.yml
Drupal\sms_user\ActiveHours

File

modules/sms_user/src/ActiveHours.php, line 13

Namespace

Drupal\sms_user
View source
class ActiveHours implements ActiveHoursInterface {

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Whether active hours is enabled in configuration.
   *
   * @var bool|null
   *   Whether active hours is enabled in configuration or NULL if configuration
   *   has not been built yet.
   */
  protected $status = NULL;

  /**
   * Date ranges as they exist in configuration.
   *
   * @var array
   *   An unsorted array containing arrays with keys 'start' and 'end' with
   *   values in strtotime() format.
   */
  protected $ranges = [];

  /**
   * Constructs a ActiveHours object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function inHours(UserInterface $user, $now = 'now') {
    $this
      ->build();

    // We're in hours if active hours feature is disabled.
    if (!$this->status) {
      return TRUE;
    }
    $timezone = $user
      ->getTimeZone();
    $now = new DrupalDateTime($now, $timezone);
    foreach ($this
      ->getRanges($timezone) as $date) {
      if ($now >= $date
        ->getStartDate() && $now <= $date
        ->getEndDate()) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function findNextTime(UserInterface $user, $now = 'now') {
    $timezone = $user
      ->getTimeZone();
    $now = new DrupalDateTime($now, $timezone);
    foreach ($this
      ->getRanges($timezone) as $date) {

      // The end date may have already passed.
      if ($now > $date
        ->getEndDate()) {
        continue;
      }
      return $date;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function delaySmsMessage(SmsMessageInterface &$sms_message) {
    $recipient = $sms_message
      ->getRecipientEntity();
    if ($sms_message
      ->isAutomated() && $recipient instanceof UserInterface) {
      if (!$this
        ->inHours($recipient) && ($range = $this
        ->findNextTime($recipient))) {
        $sms_message
          ->setSendTime($range
          ->getStartDate()
          ->format('U'));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getRanges($timezone) {
    $this
      ->build();
    $dates = [];
    foreach ($this->ranges as $range) {
      $dates[] = new ActiveHoursDates(new DrupalDateTime($range['start'], $timezone), new DrupalDateTime($range['end'], $timezone));
    }

    // Sort so nearest date is closest.
    // Can't do this in build() since computed relative dates can be different
    // per timezone.
    usort($dates, function ($a, $b) {
      if ($a
        ->getStartDate() == $b
        ->getStartDate()) {
        return 0;
      }
      return $a
        ->getStartDate() < $b
        ->getStartDate() ? -1 : 1;
    });
    return $dates;
  }

  /**
   * Store the active hours configuration state.
   */
  protected function build() {
    if (isset($this->status)) {
      return;
    }
    $settings = $this->configFactory
      ->get('sms_user.settings')
      ->get('active_hours');
    $this->status = !empty($settings['status']);
    $this->ranges = !empty($settings['ranges']) ? $settings['ranges'] : [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActiveHours::$configFactory protected property The configuration factory.
ActiveHours::$ranges protected property Date ranges as they exist in configuration.
ActiveHours::$status protected property Whether active hours is enabled in configuration.
ActiveHours::build protected function Store the active hours configuration state.
ActiveHours::delaySmsMessage public function Delay a SMS message if active hours require it to be delayed. Overrides ActiveHoursInterface::delaySmsMessage
ActiveHours::findNextTime public function Determine the next valid active hours date range for a user. Overrides ActiveHoursInterface::findNextTime
ActiveHours::getRanges public function Get ranges converted to local timezone and sorted chronologically. Overrides ActiveHoursInterface::getRanges
ActiveHours::inHours public function Determine if the current time of a user is within permitted hour ranges. Overrides ActiveHoursInterface::inHours
ActiveHours::__construct public function Constructs a ActiveHours object.