View source
<?php
declare (strict_types=1);
namespace Drupal\site_alert;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\site_alert\Entity\SiteAlert;
class CliCommands implements CliCommandsInterface {
private $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public function create(string $label, string $message, array $options) : void {
$this
->validateCreateInput($label, $message, $options);
$start = $options['start'] ?? NULL;
$end = $options['end'] ?? NULL;
$active = $options['active'] ?? NULL !== FALSE;
$severity = $this
->normalizeSeverity($options['severity'] ?? 'medium');
$storage = $this->entityTypeManager
->getStorage('site_alert');
$entity_values = [
'active' => $active,
'label' => $label,
'severity' => $severity,
'message' => $message,
];
if (!empty($start) || !empty($end)) {
$entity_values['scheduling'] = [
'value' => $start,
'end_value' => $end,
];
}
$site_alert = $storage
->create($entity_values);
$storage
->save($site_alert);
}
public function validateCreateInput(string $label, string $message, array &$options) : void {
if (empty($label) || !is_string($label)) {
throw new \InvalidArgumentException('A label is required.');
}
if (empty($message) || !is_string($message)) {
throw new \InvalidArgumentException('A message is required.');
}
foreach ([
'start',
'end',
] as $option) {
if (!empty($options[$option])) {
if (strtotime($options[$option]) === FALSE) {
throw new \InvalidArgumentException(sprintf("Invalid date format for '%s' option.", $option));
}
$options[$option] = (new DrupalDateTime($options[$option], DateTimeItemInterface::STORAGE_TIMEZONE))
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
}
}
if (isset($options['active']) && !is_bool($options['active'])) {
throw new \InvalidArgumentException("The 'active' option should be a boolean value.");
}
if (!empty($options['severity'])) {
$severity_options = array_keys(SiteAlert::SEVERITY_OPTIONS);
if (!in_array($options['severity'], $severity_options)) {
throw new \InvalidArgumentException(sprintf("The 'severity' option should be one of %s.", implode(',', $severity_options)));
}
}
}
public function delete(string $label) : int {
if (empty($label) || !is_string($label)) {
throw new \InvalidArgumentException('A label is required.');
}
$site_alerts = $this
->getAlertsByLabel($label);
$count = count($site_alerts);
$this->entityTypeManager
->getStorage('site_alert')
->delete($site_alerts);
return $count;
}
public function disable(?string $label = NULL) : int {
if (empty($label)) {
$site_alerts = $this->entityTypeManager
->getStorage('site_alert')
->loadByProperties([
'active' => 1,
]);
}
else {
$site_alerts = $this
->getAlertsByLabel($label, TRUE);
if (empty($site_alerts)) {
throw new \InvalidArgumentException(sprintf("No active site alerts found with the label '%s'.", $label));
}
}
$count = 0;
foreach ($site_alerts as $site_alert) {
$site_alert
->set('active', FALSE)
->save();
$count++;
}
return $count;
}
public function enable(string $label) : int {
$site_alerts = $this
->getAlertsByLabel($label, FALSE);
if (empty($site_alerts)) {
throw new \InvalidArgumentException(sprintf("No inactive site alerts found with the label '%s'.", $label));
}
$count = 0;
foreach ($site_alerts as $site_alert) {
$site_alert
->set('active', TRUE)
->save();
$count++;
}
return $count;
}
protected function getAlertsByLabel(string $label, ?bool $active = NULL) : array {
$site_alerts = [];
if (!empty($label)) {
$storage = $this->entityTypeManager
->getStorage('site_alert');
$query = $storage
->getQuery();
$query
->condition('label', $label, '=');
if ($active !== NULL) {
$query
->condition('active', $active);
}
$result = $query
->execute();
if (!empty($result)) {
$site_alerts = $storage
->loadMultiple($result);
}
}
return $site_alerts;
}
protected function normalizeSeverity(string $severity = 'medium') : string {
$severity = trim($severity);
$severity = strtolower($severity);
$allowed_severities = [
'low',
'medium',
'high',
];
if (!in_array($severity, $allowed_severities)) {
$severity = 'medium';
}
return $severity;
}
}