WebformScheduledEmailCommands.php in Webform 8.5
File
modules/webform_scheduled_email/src/Commands/WebformScheduledEmailCommands.php
View source
<?php
namespace Drupal\webform_scheduled_email\Commands;
use Consolidation\AnnotatedCommand\CommandData;
use Drush\Commands\DrushCommands;
use Drupal\webform\Entity\Webform;
use Drupal\webform_scheduled_email\Plugin\WebformHandler\ScheduleEmailWebformHandler;
use Drupal\webform_scheduled_email\WebformScheduledEmailManagerInterface;
class WebformScheduledEmailCommands extends DrushCommands {
protected $manager;
public function __construct(WebformScheduledEmailManagerInterface $manager) {
parent::__construct();
$this->manager = $manager;
}
public function drush_webform_scheduled_email_cron_validate(CommandData $commandData) {
$arguments = $commandData
->arguments();
$webform_id = $arguments['webform_id'];
$handler_id = $arguments['handler_id'];
$webform = NULL;
if ($webform_id) {
$webform = Webform::load($webform_id);
if (!$webform) {
throw new \Exception(dt('Webform @id not recognized.', [
'@id' => $webform_id,
]));
}
}
if ($handler_id) {
try {
$handler = $webform
->getHandler($handler_id);
} catch (\Exception $exception) {
throw new \Exception(dt('Handler @id not recognized.', [
'@id' => $handler_id,
]));
}
if (!$handler instanceof ScheduleEmailWebformHandler) {
throw new \Exception(dt('Handler @id is not a scheduled email handler.', [
'@id' => $handler_id,
]));
}
}
}
public function drush_webform_scheduled_email_cron($webform_id = NULL, $handler_id = NULL, array $options = [
'schedule_limit' => 1000,
'send_limit' => 500,
]) {
$webform = $webform_id ? Webform::load($webform_id) : NULL;
$stats = $this->manager
->cron($webform, $handler_id, $options['schedule_limit'], $options['send_limit']);
$this
->output()
->writeln(dt($stats['_message'], $stats['_context']));
}
}