commerce_amws-8.x-1.x-dev/modules/order/src/Hook/Cron.php
modules/order/src/Hook/Cron.php
<?php
namespace Drupal\commerce_amws_order\Hook;
use Drupal\commerce_amws\Entity\StoreInterface as AmwsStoreInterface;
use Drupal\entity_sync\Import\ManagerInterface as ImportManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use SellingPartnerApi\Model\Orders\Order;
/**
* Holds methods implementing hooks related to cron.
*/
class Cron {
/**
* The Entity Sync import entity manager.
*
* @var \Drupal\entity_sync\Import\ManagerInterface
*/
protected $importManager;
/**
* The Commerce AMWS Store storage.
*
* @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
*/
protected $amwsStoreStorage;
/**
* Constructs a new Cron object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
* @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.
*/
public function __construct(
ConfigFactoryInterface $config_factory,
EntityTypeManagerInterface $entity_type_manager,
ImportManagerInterface $import_manager
) {
$this->importManager = $import_manager;
$this->amwsStoreStorage = $entity_type_manager
->getStorage('commerce_amws_store');
$this->config = $config_factory->get('commerce_amws_order.settings');
}
/**
* Imports orders from Amazon MWS, if enabled in configuration.
*
* 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:
* 1. Import all orders created or updated during the last 1 day.
* 2. Orders are only created once, then skipped i.e. never updated.
* 3. Due to points 1 and 2, cron will try again to import orders that failed
* but that will affect orders already imported regardless of how
* frequently cron is run.
*
* Applications can change this behavior with their Entity Synchronization
* configurations and by overriding this service.
*/
public function import() {
if (!$this->config->get('cron.status')) {
return;
}
$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)
);
}
}
/**
* 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.
*
* @return array
* The options.
*/
protected function buildImportOptions(AmwsStoreInterface $amws_store) {
$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(),
],
],
];
// According to current convention, `NULL`, `0` or `''` means no limit. We
// therefore set the limit only if it has a non-empty value.
// See \Drupal\commerce_amws_order\Form\SettingsForm::submitForm().
// @I Set page limit accordingly
// type : improvement
// priority : low
// labels : performance
// notes : Without a page limit a full page of results will be
// requested even if we set the limit to 1.
$limit = (int) $this->config->get('cron.limit');
if ($limit) {
$options['limit'] = $limit;
}
return $options;
}
}
