public function WorkbenchEmailProcessor::processEntity in Workbench Email 8
Same name and namespace in other branches
- 2.x src/WorkbenchEmailProcessor.php \Drupal\workbench_email\WorkbenchEmailProcessor::processEntity()
Process the queue for a given entity.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: Entity to process.
File
- src/
WorkbenchEmailProcessor.php, line 66
Class
- WorkbenchEmailProcessor
- Defines a class for processing a queue for a given entity ID.
Namespace
Drupal\workbench_emailCode
public function processEntity(EntityInterface $entity) {
// Make sure every queue exists. There is no harm in trying to recreate
// an existing queue.
$queue_name = 'workbench_email_send' . PluginBase::DERIVATIVE_SEPARATOR . $entity
->getEntityTypeId();
$this->queueFactory
->get($queue_name)
->createQueue();
$queue_worker = $this->queueManager
->createInstance($queue_name, [
'entity_type' => $entity
->getEntityTypeId(),
]);
$queue = $this->queueFactory
->get($queue_name);
$to_release = [];
$end = time() + static::PROCESSING_TIME;
while (time() < $end && ($item = $queue
->claimItem())) {
if ($item->data instanceof QueuedEmail) {
// We populate and flush the queue in one request at present, so we
// should only ever have matching items. However in theory we could
// support sending in the background, in which case we are defensive
// here in case there are items in the queue that don't match the entity
// we're processing. Similarly, there may be an instance where two or
// more entities are saved in the one request, and in that case there
// would be more than one entity in the queue.
if ($item->data
->getUuid() === $entity
->uuid()) {
try {
$queue_worker
->processItem($item->data);
$queue
->deleteItem($item);
} catch (\Exception $e) {
// In case of any exception, just log it.
$this->logger
->log(RfcLogLevel::ERROR, '%type: @message in %function (line %line of %file).', Error::decodeException($e));
}
}
else {
$to_release[] = $item;
}
}
else {
throw new \LogicException('Cannot perform queue processing on objects other than a QueuedEmail.');
}
}
// Put these back into the queue.
foreach ($to_release as $item) {
$queue
->releaseItem($item);
}
}