SubmissionsCompletedSinceLastSuccess.php in Webform Scheduled Tasks 8.2
File
src/Plugin/WebformScheduledTasks/ResultSet/SubmissionsCompletedSinceLastSuccess.php
View source
<?php
namespace Drupal\webform_scheduled_tasks\Plugin\WebformScheduledTasks\ResultSet;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\State\StateInterface;
use Drupal\webform_scheduled_tasks\Iterator\WebformIteratorAggregate;
use Drupal\webform_scheduled_tasks\Plugin\WebformScheduledTasks\ResultSetPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SubmissionsCompletedSinceLastSuccess extends ResultSetPluginBase {
protected $state;
protected $time;
const STATE_KEY = 'webform_scheduled_tasks.submissions_completed_since_last_success';
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager')
->getStorage('webform_submission')
->getQuery(), $container
->get('state'), $container
->get('datetime.time'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryInterface $submissionQuery, StateInterface $state, TimeInterface $time) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $submissionQuery);
$this->state = $state;
$this->time = $time;
}
public function getResultSet() {
$this
->initializeQueryDefaults();
$success_map = $this->state
->get(static::STATE_KEY, []);
if (isset($success_map[$this
->getScheduledTask()
->id()])) {
$this->submissionQuery
->condition('completed', $success_map[$this
->getScheduledTask()
->id()], '>=');
$this->submissionQuery
->condition('completed', $this->time
->getRequestTime(), '<');
}
return WebformIteratorAggregate::createFromQuery($this->submissionQuery)
->getIterator();
}
public function onSuccess() {
$success_map = $this->state
->get(static::STATE_KEY, []);
$success_map[$this
->getScheduledTask()
->id()] = $this->time
->getRequestTime();
$this->state
->set(static::STATE_KEY, $success_map);
}
protected function getSummary() {
return $this
->t('All submissions since the last successful run of this task will be included.');
}
}