Skip to content

Instantly share code, notes, and snippets.

@IslandUsurper
Last active August 29, 2015 14:25
Show Gist options
  • Save IslandUsurper/a2dbc98ed2154682141a to your computer and use it in GitHub Desktop.
Save IslandUsurper/a2dbc98ed2154682141a to your computer and use it in GitHub Desktop.
mozu-php-sdk 1.17
<?php
// Assume appropriate `use` statements were entered.
// This is a snippet of a Drupal module, so there are some Drupal-specific functions being
// used, but they shouldn't detract from understanding what is going on.
/**
* Sets up the Mozu API context for subsequent API calls.
*/
function mozu_export_api_handshake() {
static $context = NULL;
mozu_export_api_authenticate();
if (!$context) {
$tenant_resource = new TenantResource();
$promise = $tenant_resource->getTenantAsync(variable_get('mozu_export_tenant_id'));
$tenant = $promise->wait()->json();
$context = new ApiContext($tenant);
$context->setSiteId(variable_get('mozu_export_site_id'));
$context->setMasterCatalogId(variable_get('mozu_export_master_catalog'));
$context->setCatalogId(variable_get('mozu_export_catalog_id'));
}
return $context;
}
/**
* Authenticates with the Mozu API.
*/
function mozu_export_api_authenticate() {
static $authenticator = NULL;
if (!isset($authenticator)) {
$app_auth_info = new AppAuthInfo();
$app_auth_info->sharedSecret = variable_get('mozu_export_shared_secret');
$app_auth_info->applicationId = variable_get('mozu_export_app_id');
$authenticator = AppAuthenticator::initialize($app_auth_info);
}
return $authenticator;
}
/**
* Creates the given product types in Mozu.
*/
function mozu_export_sync_product_types($types) {
$type_resource = new ProductTypeResource(mozu_export_api_handshake(), DataViewMode::LIVE);
$promises = array();
foreach ($types as $type) {
$product_type = new ProductType();
$product_type->goodsType = 'Physical';
$product_type->masterCatalogId = variable_get('mozu_export_master_catalog');
$product_type->productUsages = array('Standard', 'Configurable', 'Component');
$product_type->name = $type;
$product_type->options = array();
$product_type->extras = array();
$product_type->properties = array();
watchdog('mozu_export', 'Product type: @type', array('@type' => print_r($product_type, TRUE)), WATCHDOG_DEBUG);
$promises[$type] = $type_resource->addProductTypeAsync($product_type)->then(function (MozuResult $result) {
return $result->json()->id;
}, function (ApiException $e) {
watchdog_exception('mozu_export', $e);
watchdog('mozu_export', 'Items: @items', array('@items' => print_r($e->getItems(), TRUE)), WATCHDOG_DEBUG);
return FALSE;
});
}
return $promises;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment