/**
  * Handle possible errors and react
  * @param mixed $result
  * @return mixed
  */
 private function checkResult($result)
 {
     $this->lastError = json_last_error();
     if (JSON_ERROR_NONE !== $this->lastError) {
         IdsExceptionManager::HandleException($this->getMessageFromErrorCode($this->lastError));
     }
     //TODO add logger here
     return $result;
 }
 /**
  * This method executes the batch request.
  */
 public function Execute()
 {
     $this->serviceContext->IppConfiguration->Logger->CustomLogger->Log(TraceLevel::Info, "Started Executing Method Execute for Batch");
     // Create Intuit Batch Request
     $intuitBatchRequest = new IPPIntuitBatchRequest();
     $intuitBatchRequest->BatchItemRequest = $this->batchRequests;
     $uri = "company/{1}/batch?requestid=" . rand() . rand();
     $uri = str_replace('{1}', $this->serviceContext->realmId, $uri);
     // Creates request parameters
     $requestParameters = NULL;
     if (0) {
         // No JSON support here yet
         //$requestParameters = new RequestParameters($uri, 'POST', CoreConstants::CONTENTTYPE_APPLICATIONJSON, NULL);
     } else {
         $requestParameters = new RequestParameters($uri, 'POST', CoreConstants::CONTENTTYPE_APPLICATIONXML, NULL);
     }
     $restRequestHandler = new SyncRestHandler($this->serviceContext);
     try {
         // Get literal XML representation of IntuitBatchRequest into a DOMDocument
         $httpsPostBodyPreProcessed = XmlObjectSerializer::getPostXmlFromArbitraryEntity($intuitBatchRequest, $urlResource);
         $doc = new DOMDocument();
         $domObj = $doc->loadXML($httpsPostBodyPreProcessed);
         $xpath = new DOMXpath($doc);
         // Replace generically-named IntuitObject nodes with tags that describe contained objects
         $objectIndex = 0;
         while (1) {
             $matchingElementArray = $xpath->query("//IntuitObject");
             if (is_null($matchingElementArray)) {
                 break;
             }
             if ($objectIndex >= count($intuitBatchRequest->BatchItemRequest)) {
                 break;
             }
             foreach ($matchingElementArray as $oneNode) {
                 // Found a DOMNode currently named "IntuitObject".  Need to rename to
                 // entity that describes it's contents, like "ns0:Customer" (determine correct
                 // name by inspecting IntuitObject's class).
                 if ($intuitBatchRequest->BatchItemRequest[$objectIndex]->IntuitObject) {
                     // Determine entity name to use
                     $entityClassName = get_class($intuitBatchRequest->BatchItemRequest[$objectIndex]->IntuitObject);
                     $entityTransferName = XmlObjectSerializer::cleanPhpClassNameToIntuitEntityName($entityClassName);
                     $entityTransferName = 'ns0:' . $entityTransferName;
                     // Replace old-named DOMNode with new-named DOMNode
                     $newNode = $oneNode->ownerDocument->createElement($entityTransferName);
                     if ($oneNode->attributes->length) {
                         foreach ($oneNode->attributes as $attribute) {
                             $newNode->setAttribute($attribute->nodeName, $attribute->nodeValue);
                         }
                     }
                     while ($oneNode->firstChild) {
                         $newNode->appendChild($oneNode->firstChild);
                     }
                     $oneNode->parentNode->replaceChild($newNode, $oneNode);
                 }
                 break;
             }
             $objectIndex++;
         }
         $httpsPostBody = $doc->saveXML();
         list($responseCode, $responseBody, $responseError) = $restRequestHandler->GetResponse($requestParameters, $httpsPostBody, NULL);
     } catch (Exception $e) {
         IdsExceptionManager::HandleException($e);
     }
     CoreHelper::CheckResponseErrorAndThrowException($this->serviceContext, $responseError);
     CoreHelper::CheckNullResponseAndThrowException($responseBody);
     try {
         $this->batchResponses = array();
         $this->intuitBatchItemResponses = array();
         // No JSON support here yet
         // de serialize object
         $responseXmlObj = simplexml_load_string($responseBody);
         foreach ($responseXmlObj as $oneXmlObj) {
             // process batch item
             $intuitBatchResponse = $this->ProcessBatchItemResponse($oneXmlObj);
             $this->intuitBatchItemResponses[] = $intuitBatchResponse;
             if ($intuitBatchResponse && $intuitBatchResponse->entities && count($intuitBatchResponse->entities)) {
                 $this->batchResponses[] = $intuitBatchResponse->entities;
             }
         }
     } catch (Exception $e) {
         return NULL;
     }
     $this->serviceContext->IppConfiguration->Logger->CustomLogger->Log(TraceLevel::Info, "Finished Execute method for batch.");
 }
Example #3
0
 /**
  * Returns List of entities changed after certain time.
  * @param array entityList List of entity.
  * @param long changedSince DateTime of timespan after which entities were changed.
  * @return IntuitCDCResponse Returns an IntuitCDCResponse.
  */
 public function CDC($entityList, $changedSince)
 {
     $this->serviceContext->IppConfiguration->Logger->CustomLogger->Log(TraceLevel::Info, "Called Method CDC.");
     // Validate parameter
     if (count($entityList) <= 0) {
         $exception = new IdsException('ParameterNotNullMessage');
         $this->serviceContext->IppConfiguration->Logger->CustomLogger->Log(TraceLevel::Error, "ParameterNotNullMessage");
         IdsExceptionManager::HandleException($exception);
     }
     $entityString = implode(",", $entityList);
     $query = NULL;
     $uri = NULL;
     $formattedChangedSince = date("Y-m-d\\TH:m:sP", $this->verifyChangedSince($changedSince));
     $query = "entities=" . $entityString . "&changedSince=" . $formattedChangedSince;
     $uri = "company/{1}/cdc?{2}";
     //$uri = str_replace("{0}", CoreConstants::VERSION, $uri);
     $uri = str_replace("{1}", $this->serviceContext->realmId, $uri);
     $uri = str_replace("{2}", $query, $uri);
     // Creates request parameters
     $requestParameters = $this->getGetRequestParameters($uri, CoreConstants::CONTENTTYPE_APPLICATIONXML);
     $restRequestHandler = new SyncRestHandler($this->serviceContext);
     try {
         // gets response
         list($responseCode, $responseBody) = $restRequestHandler->GetResponse($requestParameters, NULL, NULL);
     } catch (Exception $e) {
     }
     CoreHelper::CheckNullResponseAndThrowException($responseBody);
     $returnValue = new IntuitCDCResponse();
     try {
         $xmlObj = simplexml_load_string($responseBody);
         foreach ($xmlObj->CDCResponse->QueryResponse as $oneObj) {
             $entities = $this->responseSerializer->Deserialize($oneObj->asXML(), FALSE);
             $entityName = NULL;
             foreach ($oneObj->children() as $oneObjChild) {
                 $entityName = (string) $oneObjChild->getName();
                 break;
             }
             $returnValue->entities[$entityName] = $entities;
         }
     } catch (Exception $e) {
         IdsExceptionManager::HandleException($e);
     }
     $this->serviceContext->IppConfiguration->Logger->CustomLogger->Log(TraceLevel::Info, "Finished Executing Method CDC.");
     return $returnValue;
 }
 /**
  * Returns the response headers and response code from a called OAuth object
  *
  * @param OAuth $oauth A called OAuth object
  * @return array elements are 0: HTTP response code; 1: response content, 2: HTTP response headers
  */
 private function GetOAuthResponseHeaders($oauth)
 {
     $response_code = NULL;
     $response_xml = NULL;
     $response_headers = array();
     try {
         $response_xml = $oauth->getLastResponse();
         $response_headers = array();
         $response_headers_raw = $oauth->getLastResponseHeaders();
         $response_headers_rows = explode("\r\n", $response_headers_raw);
         foreach ($response_headers_rows as $header) {
             $keyval = explode(":", $header);
             if (2 == count($keyval)) {
                 $response_headers[$keyval[0]] = trim($keyval[1]);
             }
             if (FALSE !== strpos($header, 'HTTP')) {
                 list(, $response_code, ) = explode(' ', $header);
             }
         }
         // Decompress, if applicable
         if ('QBO' == $this->context->serviceType || 'QBD' == $this->context->serviceType) {
             // Even if accept-encoding is set to deflate, server never (as far as we know) actually chooses
             // to respond with Content-Encoding: deflate.  Thus, the inspection of 'Content-Encoding' response
             // header rather than assuming that server will respond with encoding specified by accept-encoding
             if ($this->ResponseCompressor && $response_headers && array_key_exists('Content-Encoding', $response_headers)) {
                 $response_xml = $this->ResponseCompressor->Decompress($response_xml, $response_headers);
             }
         }
     } catch (Exception $e) {
         IdsExceptionManager::HandleException($e);
     }
     return array($response_code, $response_xml, $response_headers);
 }