Example #1
0
 /**
  * Builds the Shopgate Library object graph for a given ShopgatePlugin object.
  *
  * This initializes all necessary objects of the library, wires them together and injects them into
  * the plugin class via its set* methods.
  *
  * @param ShopgatePlugin $plugin The ShopgatePlugin instance that should be wired to the framework.
  */
 public function buildLibraryFor(ShopgatePlugin $plugin)
 {
     // set error handler if configured
     if ($this->config->getUseCustomErrorHandler()) {
         set_error_handler('ShopgateErrorHandler');
     }
     // instantiate API stuff
     // -> MerchantAPI auth service (needs to be initialized first, since the config still can change along with the authentication information
     switch ($this->config->getSmaAuthServiceClassName()) {
         case ShopgateConfigInterface::SHOPGATE_AUTH_SERVICE_CLASS_NAME_SHOPGATE:
             $smaAuthService = new ShopgateAuthenticationServiceShopgate($this->config->getCustomerNumber(), $this->config->getApikey());
             $smaAuthService->setup($this->config);
             $merchantApi = new ShopgateMerchantApi($smaAuthService, $this->config->getShopNumber(), $this->config->getApiUrl());
             break;
         case ShopgateConfigInterface::SHOPGATE_AUTH_SERVICE_CLASS_NAME_OAUTH:
             $smaAuthService = new ShopgateAuthenticationServiceOAuth($this->config->getOauthAccessToken());
             $smaAuthService->setup($this->config);
             $merchantApi = new ShopgateMerchantApi($smaAuthService, null, $this->config->getApiUrl());
             break;
         default:
             // undefined auth service
             return trigger_error('Invalid SMA-Auth-Service defined - this should not happen with valid plugin code', E_USER_ERROR);
     }
     // -> PluginAPI auth service (currently the plugin API supports only one auth service)
     $spaAuthService = new ShopgateAuthenticationServiceShopgate($this->config->getCustomerNumber(), $this->config->getApikey());
     $pluginApi = new ShopgatePluginApi($this->config, $spaAuthService, $merchantApi, $plugin);
     if ($this->config->getExportConvertEncoding()) {
         array_splice(ShopgateObject::$sourceEncodings, 1, 0, $this->config->getEncoding());
         ShopgateObject::$sourceEncodings = array_unique(ShopgateObject::$sourceEncodings);
     }
     if ($this->config->getForceSourceEncoding()) {
         ShopgateObject::$sourceEncodings = array($this->config->getEncoding());
     }
     // instantiate export file buffer
     if (!empty($_REQUEST['action']) && ($_REQUEST['action'] == 'get_items' || $_REQUEST['action'] == 'get_categories' || $_REQUEST['action'] == 'get_reviews')) {
         $xmlModelNames = array('get_items' => 'Shopgate_Model_Catalog_Product', 'get_categories' => 'Shopgate_Model_Catalog_Category', 'get_reviews' => 'Shopgate_Model_Review');
         $format = !empty($_REQUEST['response_type']) ? $_REQUEST['response_type'] : '';
         switch ($format) {
             default:
             case 'xml':
                 /* @var $xmlModel Shopgate_Model_AbstractExport */
                 $xmlModel = new $xmlModelNames[$_REQUEST['action']]();
                 $xmlNode = new Shopgate_Model_XmlResultObject($xmlModel->getItemNodeIdentifier());
                 $fileBuffer = new ShopgateFileBufferXml($xmlModel, $xmlNode, $this->config->getExportBufferCapacity(), $this->config->getExportConvertEncoding(), ShopgateObject::$sourceEncodings);
                 break;
             case 'json':
                 $fileBuffer = new ShopgateFileBufferJson($this->config->getExportBufferCapacity(), $this->config->getExportConvertEncoding(), ShopgateObject::$sourceEncodings);
                 break;
         }
     } else {
         if (!empty($_REQUEST['action']) && ($_REQUEST['action'] == 'get_items_csv' || $_REQUEST['action'] == 'get_categories_csv' || $_REQUEST['action'] == 'get_reviews_csv')) {
             $fileBuffer = new ShopgateFileBufferCsv($this->config->getExportBufferCapacity(), $this->config->getExportConvertEncoding(), ShopgateObject::$sourceEncodings);
         } else {
             $fileBuffer = new ShopgateFileBufferCsv($this->config->getExportBufferCapacity(), $this->config->getExportConvertEncoding(), ShopgateObject::$sourceEncodings);
         }
     }
     // inject apis into plugin
     $plugin->setConfig($this->config);
     $plugin->setMerchantApi($merchantApi);
     $plugin->setPluginApi($pluginApi);
     $plugin->setBuffer($fileBuffer);
 }