commerce_amws-8.x-1.x-dev/modules/order/src/EntitySync/OrderClient.php

modules/order/src/EntitySync/OrderClient.php
<?php

namespace Drupal\commerce_amws_order\EntitySync;

use Drupal\commerce_amws\Api\ListClientInterface;
use Drupal\entity_sync\Client\ClientInterface as EntitySyncClientInterface;

/**
 * Entity Sync client adapter for order-related requestes.
 */
class OrderClient implements EntitySyncClientInterface {

  /**
   * The API client.
   *
   * @var \Drupal\commerce_amws\Api\ListClientInterface
   */
  protected $client;

  /**
   * Costructs a new OrderClient object.
   *
   * @param \Drupal\commerce_amws\Api\ListClientInterface $client
   *   The API client.
   */
  public function __construct(ListClientInterface $client) {
    $this->client = $client;
  }

  /**
   * {@inheritdoc}
   */
  public function importList(array $filters = [], array $options = []) {
    return $this->client->list(
      ['limit' => $options['limit'] ?? ListClientInterface::DEFAULT_LIMIT],
      $this->buildQuery($filters, $options)
    );
  }

  /**
   * {@inheritdoc}
   */
  public function importEntity($id) {
    throw new \Exception('Importing an individual order from Amazon MWS is not supported yet.');
  }

  /**
   * {@inheritdoc}
   */
  public function create(array $fields) {
    throw new \Exception('Creating an order on Amazon MWS is not supported yet.');
  }

  /**
   * {@inheritdoc}
   */
  public function update($id, array $fields) {
    throw new \Exception('Updating an order on Amazon MWS is not supported yet.');
  }

  /**
   * Builds the query parameters as expected by the Sync API SDK.
   *
   * @param array $filters
   *   The filters supported by the Entity Sync client.
   * @param array $options
   *   The array of options provided by the import entity manager.
   *
   * @return array
   *   The query parameters.
   */
  protected function buildQuery(array $filters, array $options) {
    $query = [];

    // Time filters.
    $keys = [
      'created_start' => 'created_after',
      'created_end' => 'created_before',
      'changed_start' => 'last_updated_after',
      'changed_end' => 'last_updated_before',
    ];
    foreach ($keys as $sync_key => $amws_key) {
      if (isset($filters[$sync_key])) {
        $query[$amws_key] = $this->timestampToIso8601($filters[$sync_key]);
      }
    }

    // Custom query parameters.
    $query += $options['parameters'] ?? [];

    return $query;
  }

  /**
   * Converts a Unix timestamp to an ISO 8601 date as expected by the SP-API.
   *
   * @param int $timestamp
   *   The Unix timestamp to convert.
   *
   * @return string
   *   The time in ISO 8601 format and in UTC timezone.
   *
   * @I Replace with \Drupal\commerce_amws\Utilities::timestampToIso8601
   *    type     : task
   *    priority : normal
   *    labels   : modularity
   */
  protected function timestampToIso8601($timestamp) {
    $datetime = new \DateTime('now', new \DateTimeZone('UTC'));
    $datetime->setTimestamp($timestamp);

    return $datetime->format('Y-m-d\TH:i:s') . 'Z';
  }

}

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

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