commerce_amws-8.x-1.x-dev/modules/order/src/Commands/Commands.php
modules/order/src/Commands/Commands.php
<?php
namespace Drupal\commerce_amws_order\Commands;
use Drupal\commerce_amws\Entity\StoreInterface as AmwsStoreInterface;
use Drupal\commerce_amws_order\OrderPurgerInterface;
use Drupal\entity_sync\Import\ManagerInterface as ImportManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drush\Commands\DrushCommands;
use SellingPartnerApi\Model\Orders\Order;
/**
* Drush commands that provide operations related to Amazon MWS orders.
*/
class Commands extends DrushCommands {
/**
* The Amazon MWS order purger service.
*
* @var \Drupal\commerce_amws_order\OrderPurgerInterface
*/
protected $amwsOrderPurger;
/**
* The Entity Sync import entity manager.
*
* @var \Drupal\entity_sync\Import\ManagerInterface
*/
protected $importManager;
/**
* The Amazon MWS store storage.
*
* @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
*/
protected $amwsStoreStorage;
/**
* Constructs a new Commands object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\entity_sync\Import\ManagerInterface $import_manager
* The Entity Sync import entity manager.
* @param \Drupal\commerce_amws_order\OrderPurgerInterface $amws_order_purger
* The Amazon MWS order purger service.
*/
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
ImportManagerInterface $import_manager,
OrderPurgerInterface $amws_order_purger
) {
$this->amwsOrderPurger = $amws_order_purger;
$this->importManager = $import_manager;
$this->amwsStoreStorage = $entity_type_manager
->getStorage('commerce_amws_store');
}
/**
* Imports orders for all enabled Amazon MWS stores.
*
* For backwards compatibility we keep the same behavior we had in `1.x`. See
* \Drupal\commerce_amws_order\Hook\Cron::import().
*
* @command commerce-amws-order:import-orders
*
* @option $limit
* A positive integer number limiting the number of orders to import per
* store.
*
* @validate-module-enabled commerce_amws_order
*
* @aliases camwso:import-orders, camwso-io
*
* @usage commerce-amws-order:import-orders
* Import all Amazon MWS orders created or updated within the last day for
* all enabled Amazon MWS stores.
* @usage commerce-amws-order:import-orders --limit=12
* Import maximum 12 Amazon MWS orders created or updated within the last
* day for all enabled Amazon MWS stores.
*
* @I Refactor code to a service
* type : task
* priority : low
* labels : coding-standards
* notes : Code duplication with Cron hook service.
*/
public function importOrders(array $options = ['limit' => '0']) {
$amws_store_ids = $this->amwsStoreStorage
->getQuery()
->accessCheck(FALSE)
->condition('status', AmwsStoreInterface::STATUS_PUBLISHED)
->execute();
if (!$amws_store_ids) {
return;
}
$amws_stores = $this->amwsStoreStorage->loadMultiple($amws_store_ids);
foreach ($amws_stores as $amws_store) {
$this->importManager->importRemoteList(
'commerce_amws__order',
$this->buildImportFilters(),
$this->buildImportOptions($amws_store, (int) $options['limit'])
);
}
}
/**
* Purges orders after a set period of time.
*
* @param array $options
* An associative array of options whose values come from cli, aliases,
* config, etc.
*
* @command commerce-amws-order:purge-orders
*
* @option $interval
* The time interval in seconds (i.e. an integer number) counted from the
* time orders were created (imported) after which orders will be
* deleted. If 0 is provided, all orders will be deleted. If no interval is
* provided, the time interval defined in the configuration will be used
* which must be defined.
* @option $limit
* A positive integer number limiting the number of orders to purge.
* @option $force
* Indicates that the purge should be run even when order purging is
* disabled in the configuration.
*
* @validate-module-enabled commerce_amws_order
*
* @aliases camwso:purge-orders, camwso-po
*
* @usage commerce-amws-order:purge-orders
* Purge all Amazon MWS orders older than the time interval defined in the
* configuration.
* @usage commerce-amws-order:purge-orders --limit=12
* Purge the oldest 12 Amazon MWS orders older than the time interval
* defined in the configuration.
* @usage commerce-amws-order:purge-orders --interval=86400
* Purge all Amazon MWS orders older than 30 days.
* @usage commerce-amws-order:purge-orders --force --interval=86400
* Purge all Amazon MWS orders older than 30 days even if order purging is
* disabled in the configuration.
*
* @I Make the puge options configurable per store
* type : improvement
* priority : low
* labels : order, purge
* notes : We need to store a reference to the AMWS store on an order
* field. We can then move the purge settings to each
* store. That might be useful if data privacy and security
* requirements are different in different countries.
*/
public function purgeOrders(array $options = [
'interval' => NULL,
'limit' => '0',
'force' => FALSE,
]) {
if ($options['interval'] !== NULL) {
$options['interval'] = (int) $options['interval'];
}
$purge_options = [
'interval' => $options['interval'],
'limit' => (int) $options['limit'],
'force' => $options['force'],
];
$this->amwsOrderPurger->purge($purge_options, FALSE);
}
/**
* Builds the filters to pass to the import entity manager.
*
* @return array
* The filters.
*/
protected function buildImportFilters() {
return [
'changed_start' => strtotime('-1 day'),
];
}
/**
* Returns the options to pass to the import entity manager.
*
* For backwards compatibility, and until we better define what works better
* for more use cases, we keep the same behavior we had in `1.x`. That is:
* - We import only orders in Unshipped status.
* - We import only merchant-fulfilled orders.
*
* @param \Drupal\commerce_amws\Entity\StoreInterface $amws_store
* The Commerce AMWS store for which we are importing the orders.
* @param int $limit
* The maximum number of orders to import. Set to 0 for no limit.
*
* @return array
* The options.
*/
protected function buildImportOptions(
AmwsStoreInterface $amws_store,
int $limit
) {
$options = [
'context' => [
'amws_store_id' => $amws_store->id(),
],
'client' => [
'parameters' => [
'statuses' => [Order::ORDER_STATUS_UNSHIPPED],
'fulfillment_channels' => [Order::FULFILLMENT_CHANNEL_MFN],
],
],
'client_config' => [
'parameters' => [
'amws_store_id' => $amws_store->id(),
],
],
];
if ($limit) {
$options['limit'] = $limit;
}
return $options;
}
}
