Commit 9c725634 authored by Dimitris Bozelos's avatar Dimitris Bozelos
Browse files

Issue #3308487 Skip exports of shipments that are not shipped yet

parent cff2a019
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -17,12 +17,17 @@ services:
    tags:
      - { name: event_subscriber }

  commerce_amws_shipping.shipment_export_local_entity_mapping:
  commerce_amws_shipping.export.shipment_validate:
    class: Drupal\commerce_amws_shipping\EventSubscriber\ShipmentExportValidate
    tags:
      - { name: event_subscriber }

  commerce_amws_shipping.export.shipment_local_entity_mapping:
    class: Drupal\commerce_amws_shipping\EventSubscriber\ShipmentExportLocalEntityMapping
    tags:
      - { name: event_subscriber }

  commerce_amws_shipping.shipment_export_local_entity_terminate:
  commerce_amws_shipping.export.shipment_local_entity_terminate:
    class: Drupal\commerce_amws_shipping\EventSubscriber\ShipmentExportLocalEntityTerminate
    arguments:
      - '@entity_type.manager'
+62 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\commerce_amws_shipping\EventSubscriber;

use Drupal\commerce_amws_shipping\Plugin\EntitySync\OperationConfigurator\FeedPostOrderFulfillmentData;
use Drupal\entity_sync\Entity\OperationTypeInterface;
use Drupal\entity_sync\Event\PreInitiateOperationEvent;
use Drupal\entity_sync\Export\Event\Events;
use Drupal\entity_sync\MachineName\Field\Operation as OperationField;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Validates that the shipment has been shipped.
 */
class ShipmentExportValidate implements EventSubscriberInterface {

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

  /**
   * Validates that the shipment being exported is in Shipped state.
   *
   * The Entity Synchronization state manager takes care of triggering the
   * export when a shipment is created and when any of the exportable fields
   * (state and tracking codes) change.
   *
   * The purpose of the export is to send fulfillment data i.e. carrier
   * information and tracking codes to Amazon MWS. If the item has not been
   * shipped there's no information to send and, in fact, the feed submissions
   * will be rejected.
   *
   * If validation here fails, the export operation is cancelled.
   *
   * @param \Drupal\entity_sync\Event\PreInitiateOperationEvent $event
   *   The preinitiate operation event.
   */
  public function validate(PreInitiateOperationEvent $event) {
    $operation_type = $event->getSync();
    if (!$operation_type instanceof OperationTypeInterface) {
      return;
    }

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

    $shipment = $event->getContext()['local_entity'];
    if ($shipment->get(OperationField::STATE)->first()->getId() !== 'shipped') {
      $event->cancel();
    }
  }

}