class ActiveHours in SMS Framework 8
Same name and namespace in other branches
- 2.x modules/sms_user/src/ActiveHours.php \Drupal\sms_user\ActiveHours
- 2.1.x modules/sms_user/src/ActiveHours.php \Drupal\sms_user\ActiveHours
Defines the user active hours service.
Hierarchy
- class \Drupal\sms_user\ActiveHours implements ActiveHoursInterface
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_userView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ActiveHours:: |
protected | property | The configuration factory. | |
ActiveHours:: |
protected | property | Date ranges as they exist in configuration. | |
ActiveHours:: |
protected | property | Whether active hours is enabled in configuration. | |
ActiveHours:: |
protected | function | Store the active hours configuration state. | |
ActiveHours:: |
public | function |
Delay a SMS message if active hours require it to be delayed. Overrides ActiveHoursInterface:: |
|
ActiveHours:: |
public | function |
Determine the next valid active hours date range for a user. Overrides ActiveHoursInterface:: |
|
ActiveHours:: |
public | function |
Get ranges converted to local timezone and sorted chronologically. Overrides ActiveHoursInterface:: |
|
ActiveHours:: |
public | function |
Determine if the current time of a user is within permitted hour ranges. Overrides ActiveHoursInterface:: |
|
ActiveHours:: |
public | function | Constructs a ActiveHours object. |