Skip to content
Snippets Groups Projects
Commit f905674e authored by Dimitris Bozelos's avatar Dimitris Bozelos
Browse files

Issue #3354752 Added runner for list operations

parent 8ccceeb9
Branches
Tags
No related merge requests found
......@@ -15,6 +15,14 @@ services:
- '@datetime.time'
- '@entity_type.manager'
## Runners.
acumatica.entity_sync_runner.entity_list:
class: Drupal\acumatica\EntitySync\Runner\EntityList
arguments:
- '@entity_type.manager'
- '@logger.channel.acumatica'
- '@entity_sync.import.manager'
## Subscribers.
acumatica.entity_sync_subscriber.variation_import_remote_entity_mapping:
class: Drupal\acumatica\EventSubscriber\VariationImportRemoteEntityMapping
......
<?php
declare(strict_types=1);
namespace Drupal\acumatica\EntitySync\Runner;
use Drupal\entity_sync\MachineName\Field\Operation as OperationField;
use Drupal\entity_sync\Entity\OperationInterface;
use Drupal\entity_sync\Entity\Runner\RunnerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Psr\Log\LoggerInterface;
/**
* Base class for runnners.
*
* All operations have the same workflow by default. We provide here code that
* wraps the operation execution and handles state transitions.
*/
abstract class Base implements RunnerInterface {
/**
* Runs the operation.
*
* The `run` method handles state transitions. Child classes must define the
* actual execution of the operation by implementing this method.
*
* @param \Drupal\entity_sync\Entity\OperationInterface $operation
* The operation to run.
* @param array $context
* Additional context that the runner may need for running the operation.
*/
abstract protected function doRun(
OperationInterface $operation,
array $context
): void;
/**
* Constructs a new Base object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Psr\Log\LoggerInterface $logger
* The module's logger channel.
*/
public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected LoggerInterface $logger
) {
}
/**
* {@inheritdoc}
*/
public function run(OperationInterface $operation, array $context = []) {
$transition_id = 'complete';
try {
$this->doRun($operation, $context);
}
catch (\Throwable $throwable) {
$transition_id = 'fail';
$this->logger->error(
'Operation with ID "@operation_id" failed to run: @throwable @message',
[
'@operation_id' => $operation->id(),
'@throwable' => get_class($throwable),
'@message' => $throwable->getMessage(),
]
);
}
$operation
->get(OperationField::STATE)
->first()
->applyTransitionById($transition_id);
$this->entityTypeManager
->getStorage('entity_sync_operation')
->save($operation);
}
}
<?php
declare(strict_types=1);
namespace Drupal\acumatica\EntitySync\Runner;
use Drupal\entity_sync\Entity\OperationInterface;
use Drupal\entity_sync\Import\ManagerInterface as ImportManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Psr\Log\LoggerInterface;
/**
* Runner for operation configurator plugins that operate on lists of entities.
*
* Currently only supports importing entities. Support for exporting entities
* will be added here.
*/
class EntityList extends Base {
/**
* Constructs a new EntityList object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Psr\Log\LoggerInterface $logger
* The module's logger channel.
* @param \Drupal\entity_sync\Import\ManagerInterface $importManager
* The Entity Synchronization entity import manager.
*/
public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected LoggerInterface $logger,
protected ImportManagerInterface $importManager
) {
}
/**
* {@inheritdoc}
*/
protected function doRun(
OperationInterface $operation,
array $context
): void {
$this->importManager->importRemoteList(
$operation->bundle(),
$context['filters'],
$context['options']
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment