commerce_amws-8.x-1.x-dev/modules/shipping/src/EventSubscriber/ShipmentExportLocalEntityTerminate.php

modules/shipping/src/EventSubscriber/ShipmentExportLocalEntityTerminate.php
<?php

namespace Drupal\commerce_amws_shipping\EventSubscriber;

use Drupal\commerce_amws\MachineName\Field\Operation as AmwsOperationField;
use Drupal\commerce_amws_shipping\Plugin\EntitySync\OperationConfigurator\FeedPostOrderFulfillmentData;

use Drupal\entity_sync\Entity\OperationTypeInterface;
use Drupal\entity_sync\Event\TerminateOperationEvent;
use Drupal\entity_sync\Export\Event\Events;
use Drupal\entity_sync\MachineName\Field\Operation as OperationField;

use Drupal\Core\Entity\EntityTypeManagerInterface;

use SellingPartnerApi\Model\Feeds\Feed;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Updates the local entity after the export local entity terminate event.
 */
class ShipmentExportLocalEntityTerminate implements EventSubscriberInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new DefaultImportRemoteEntityMapping object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

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

  /**
   * Store the remote IDs and the remote state on the operation entity.
   *
   * @param \Drupal\entity_sync\Event\TerminateOperationEvent $event
   *   The terminate operation event.
   */
  public function updateOperation(TerminateOperationEvent $event) {
    $operation_type = $event->getSync();
    if (!$operation_type instanceof OperationTypeInterface) {
      return;
    }

    $plugin = $operation_type->getPlugin();
    if (!$plugin instanceof FeedPostOrderFulfillmentData) {
      return;
    }

    $response = $event->getData()['response'];
    $operation = $event->getContext()['operation_entity'];

    // phpcs:disable
    // @I Handle errors if the submission failed or was not accepted
    //    type     : bug
    //    priority : normal
    //    labels   : entity-sync, error-handling
    // phpcs:enable
    $operation->set(
      OperationField::REMOTE_ID,
      $response->feedId
    );
    $operation->set(
      AmwsOperationField::FEED_DOCUMENT_ID,
      $response->feedDocumentId
    );
    $operation->set(
      OperationField::REMOTE_STATE,
      Feed::PROCESSING_STATUS_IN_QUEUE
    );

    $this->entityTypeManager
      ->getStorage('entity_sync_operation')
      ->save($operation);
  }

}

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

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