class FormAssemblyBatchProcessor in FormAssembly 8
The "business logic" of the batch process for syncing with FormAssembly.
Used by the core batch callbacks in formassembly.module and both the drush and drupal console commands.
@author Shawn P. Duncan <code@sd.shawnduncan.org>
Copyright 2019 by Shawn P. Duncan. This code is released under the GNU General Public License. Which means that it is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. http://www.gnu.org/licenses/gpl.html @package Drupal\formassembly
Hierarchy
- class \Drupal\formassembly\FormAssemblyBatchProcessor uses StringTranslationTrait
Expanded class hierarchy of FormAssemblyBatchProcessor
2 files declare their use of FormAssemblyBatchProcessor
- FormassemblyCommands.php in src/
Commands/ FormassemblyCommands.php - SyncCommand.php in src/
Command/ SyncCommand.php
1 string reference to 'FormAssemblyBatchProcessor'
1 service uses FormAssemblyBatchProcessor
File
- src/
FormAssemblyBatchProcessor.php, line 26
Namespace
Drupal\formassemblyView source
class FormAssemblyBatchProcessor {
use StringTranslationTrait;
/**
* Injected Sync service.
*
* @var \Drupal\formassembly\ApiSync
*/
protected $apiSync;
/**
* Injected UUID service.
*
* @var \Drupal\Component\Uuid\Php
*/
protected $uuid;
/**
* FormAssemblyBatchProcessor constructor.
*
* @param \Drupal\formassembly\ApiSync $apiSync
* Injected service.
* @param \Drupal\Component\Uuid\UuidInterface $uuid
* Injected service.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* Injected service.
*/
public function __construct(ApiSync $apiSync, UuidInterface $uuid, TranslationInterface $string_translation) {
$this->apiSync = $apiSync;
$this->uuid = $uuid;
$this->stringTranslation = $string_translation;
}
/**
* Configures the batch with unique id.
*
* @param array $config
* An array with batch configuration options.
*/
public function configureBatch(array &$config) {
/*
* We need to provide a unique key to the sync service for this batch.
* The service will use the key to cache results between calls.
*/
$config['sandbox']['sync_id'] = $this->uuid
->generate();
}
/**
* Processes one iteration of the batch process.
*
* @param array $batchConfig
* An array with batch configuration options.
*
* @throws \Exception
*/
public function iterateBatch(array &$batchConfig) {
// Batch has started processing. Get the cache id back from context.
$sync_id = $batchConfig['sandbox']['sync_id'];
try {
$batchConfig['message'] = t('Requesting page %page of forms.', [
'%page' => $this->apiSync
->getPage(),
]);
if ($this->apiSync
->getForms($sync_id)) {
// Returns true when finished.
$batchConfig['finished'] = 1;
$batchConfig['results']['sync_id'] = $sync_id;
}
else {
$batchConfig['finished'] = 0.66;
}
} catch (\Exception $caught) {
\Drupal::logger('Formassembly')
->error('Form batch request failed. Error message: @message', [
'@message' => $caught
->getMessage(),
]);
throw $caught;
}
}
/**
* Completes the batch process business logic.
*
* @param string $sync_id
* The ID assigned to this sync operation.
*
* @throws \Exception
*/
public function batchPostProcess($sync_id) {
if (!empty($sync_id)) {
$this->apiSync
->syncForms($sync_id);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FormAssemblyBatchProcessor:: |
protected | property | Injected Sync service. | |
FormAssemblyBatchProcessor:: |
protected | property | Injected UUID service. | |
FormAssemblyBatchProcessor:: |
public | function | Completes the batch process business logic. | |
FormAssemblyBatchProcessor:: |
public | function | Configures the batch with unique id. | |
FormAssemblyBatchProcessor:: |
public | function | Processes one iteration of the batch process. | |
FormAssemblyBatchProcessor:: |
public | function | FormAssemblyBatchProcessor constructor. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |