commerce_amws-8.x-1.x-dev/modules/order/src/EventSubscriber/OrderImportTriggerDependentSyncs.php

modules/order/src/EventSubscriber/OrderImportTriggerDependentSyncs.php
<?php

namespace Drupal\commerce_amws_order\EventSubscriber;

use Drupal\entity_sync\Import\Event\Events;
use Drupal\entity_sync\Event\TerminateOperationEvent;
use Drupal\entity_sync\Import\ManagerInterface;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Triggers dependent synchronizations associated with order imports.
 */
class OrderImportTriggerDependentSyncs implements
  EventSubscriberInterface {

  /**
   * The Entity Sync import manager service.
   *
   * @var \Drupal\entity_sync\Import\ManagerInterface
   */
  protected $manager;

  /**
   * Constructs a new ManagedImportUsersTriggerDependentSyncs instance.
   *
   * @param \Drupal\entity_sync\Import\ManagerInterface $manager
   *   The Entity Sync import manager service.
   */
  public function __construct(ManagerInterface $manager) {
    $this->manager = $manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      Events::REMOTE_ENTITY_TERMINATE => ['triggerEntityDependentSyncs', 100],
    ];
    return $events;
  }

  /**
   * Trigger dependent synchronizations for the order import.
   *
   * Currently, we trigger import of the order's items.
   *
   * @param \Drupal\entity_sync\Event\TerminateOperationEvent $event
   *   The terminate operation event.
   */
  public function triggerEntityDependentSyncs(TerminateOperationEvent $event) {
    if (!$this->isApplicable($event)) {
      return;
    }

    $context = $event->getContext();

    // Trigger the order item synchronization. Keep the same context as the
    // initialorder synchronization, and add a `triggered_by` context so that
    // any subscribers will know that this is a dependent synchronization.
    $context = $context + [
      'amws_order_id' => $context['remote_entity']->AmazonOrderId,
      'triggered_by' => $event->getSync()->get('id'),
      'commerce_order' => $event->getData()['local_entity'],
    ];
    $this->manager->importRemoteList(
      'commerce_amws__order_item',
      [],
      [
        'context' => $context,
        'client' => [
          'parameters' => [
            'order_id' => $context['remote_entity']->AmazonOrderId,
          ],
        ],
        'client_config' => [
          'parameters' => [
            'amws_store_id' => $context['amws_store_id'],
          ],
        ],
      ]
    );
  }

  /**
   * Returns whether the dependent synchronizations should be triggered or not.
   *
   * The dependent synchronizations should be triggered if we are importing an
   * individual order, and the import action is either to create or to update
   * the order i.e. not to skip.
   *
   * @param \Drupal\entity_sync\Event\TerminateOperationEvent $event
   *   The terminate operation event.
   *
   * @return bool
   *   Whether the dependent synchronizations should be triggered.
   */
  protected function isApplicable(TerminateOperationEvent $event) {
    if ($event->getOperation() !== 'import_entity') {
      return FALSE;
    }

    $sync = $event->getSync();
    if ($sync->get('id') !== 'commerce_amws__order') {
      return FALSE;
    }

    // We may have the import set to be skipped either by a custom event
    // subscriber or in configuration (`create_entities` or `update_entities`)
    // set to FALSE. There was no order imported in that case and we don't want
    // to import its order items either.
    $data = $event->getData();
    $actions = [
      ManagerInterface::ACTION_CREATE,
      ManagerInterface::ACTION_UPDATE,
    ];
    if (!in_array($data['action'], $actions, TRUE)) {
      return FALSE;
    }

    return TRUE;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc