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

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

namespace Drupal\commerce_amws_shipping\EventSubscriber;

use Drupal\commerce_amws\MachineName\Field\Order as OrderField;

use Drupal\entity_sync\Exception\EntityExportException;
use Drupal\entity_sync\Export\Event\Events;
use Drupal\entity_sync\Export\Event\LocalEntityMappingEvent;
use Drupal\entity_sync\Export\EntityManagerInterface;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Builds the remote ID mapping for shipment export operations.
 */
class ShipmentExportLocalEntityMapping implements EventSubscriberInterface {

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

  /**
   * Builds the remote ID mapping for shipment export operations.
   *
   * @param \Drupal\entity_sync\Export\Event\LocalEntityMappingEvent $event
   *   The local entity export mapping event.
   */
  public function buildEntityMapping(LocalEntityMappingEvent $event) {
    $sync = $event->getSync();

    // We don't know the ID of the synchronization or operation type. We
    // therefore determine whether this is a shipment export by the client
    // factory.
    // phpcs:disable
    // @I Check the configurator plugin type as in terminate subscriber
    //    type     : bug
    //    priority : normal
    //    labels   : entity-sync
    // phpcs:enable
    $settings = $sync->getRemoteResourceSettings();
    if (empty($settings['client'])) {
      return;
    }
    if ($settings['client']['type'] !== 'factory') {
      return;
    }
    if ($settings['client']['service'] !== 'commerce_amws_shipping.entity_sync_client.feed_post_order_fulfillment_data_factory') {
      return;
    }

    $shipment = $event->getLocalEntity();
    $order = $shipment->getOrder();
    if (!$order) {
      throw new EntityExportException(sprintf(
        'No order found for shipment with ID "%s".',
        $shipment->id()
      ));
    }

    $amws_store_field = $order->get(OrderField::AMWS_STORE);
    if ($amws_store_field->isEmpty()) {
      throw new EntityExportException(sprintf(
        'Order with ID "%s" does not belong to an Amazon MWS store.',
        $order->id()
      ));
    }

    $amws_store = $amws_store_field->entity;
    if (!$amws_store) {
      throw new EntityExportException(sprintf(
        'Order with ID "%s" belongs a non-existing Amazon MWS store with ID "%s".',
        $order->id(),
        $amws_store_field->target_id
      ));
    }

    // We always create a new Feed i.e. we always have a `create` action.
    $entity_mapping = [
      'action' => EntityManagerInterface::ACTION_CREATE,
      'client' => $settings['client'],
    ];
    $entity_mapping['client']['parameters'] = ['amws_store' => $amws_store];
    $event->setEntityMapping($entity_mapping);
  }

}

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

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