fromArray(array( 'xmlRpcClient' => $this->client, 'valuation' => $this->valuation, 'callbackUrl' => $this->client->callbackUrl, 'methodName' => $methodName, 'packet' => serialize($packet), 'response' => serialize($response), 'status' => $status )); $this->em->persist($log); $this->em->flush($log); } /** * Put together a client-friendly valuation packet. * @param \Valuation\Entity\Valuation $valuation * @return struct */ protected function prepareCallbackData($methodName, $valuation) { $damages = array(); $items = $valuation->stock->damages->getIterator(); foreach ($items as $item) { $damages[] = array( 'VehicleComponentId' => $item->damage->id, 'Amount' => $item->amount ); } $accessories = array(); $items = $valuation->stock->accessories->getIterator(); foreach ($items as $item) { $accessories[] = array( 'VehicleAccessoryId' => $item->accessory->id ); } $data = array( // Valuation data. 'CustomerName' => $valuation->firstName, 'CustomerSurname' => $valuation->familyName, 'CustomerID' => $valuation->idNumber, 'Mobile' => $valuation->mobile, 'Email' => $valuation->email, 'Department' => $valuation->department, 'AmountOffered' => $valuation->amountOffered, 'VehicleYearId' => $valuation->stock->vehicleYear->id, 'VehicleTypeId' => $valuation->stock->type->id, 'RegistrationNumber' => $valuation->stock->registrationNumber, 'VehicleFuelTypeId' => $valuation->stock->fuelType->id, 'VehicleTransmissionTypeId' => $valuation->stock->transmissionType->id, 'VinNumber' => $valuation->stock->vinNumber, 'EngineNumber' => $valuation->stock->engineNumber, 'Km' => $valuation->stock->km, 'VehicleConditionId' => !is_null($valuation->stock->condition) ? $valuation->stock->condition->id : null, 'VehicleExteriorColourId' => !is_null($valuation->stock->exteriorColour) ? $valuation->stock->exteriorColour->id : null, 'VehicleInteriorColourId' => !is_null($valuation->stock->interiorColour) ? $valuation->stock->interiorColour->id : null, 'VehicleUpholsteryId' => !is_null($valuation->stock->upholstery) ? $valuation->stock->upholstery->id : null, 'VehiclePapersId' => !is_null($valuation->stock->papers) ? $valuation->stock->papers->id : null, 'VehicleNatisId' => !is_null($valuation->stock->natis) ? $valuation->stock->natis->id : null, 'FullServiceHistory' => $valuation->stock->fullServiceHistory, 'FullServiceHistoryNotes' => $valuation->stock->fshNotes, 'PreviousRepairs' => $valuation->stock->previousRepairsNoted, 'PreviousRepairsNotes' => $valuation->stock->previousRepairsNotes, 'AccessoryNotes' => $valuation->stock->accessoryNotes, 'DamageNotes' => $valuation->stock->damageNotes, 'Damages' => $damages, 'Accessories' => $accessories ); return $data; } /** * Do SentToSales client system callback. * @param integer $valuationId */ public function triggerSentToSales($valuationId) { $this->em = \Utility\Registry::getEntityManager(); $this->valuation = $this->em->find('Valuation\\Entity\\Valuation', $valuationId); $this->client = $this->valuation->xmlRpcClient; $client = new \Zend\XmlRpc\Client( $this->client->callbackUrl ); #-> Prepare data. $data = $this->prepareCallbackData('SentToSales', $this->valuation); $packet = array( 'Email' => $this->valuation->createdBy->email, 'ItemId' => $this->valuation->sfItemId, 'Valuation' => $data ); try { #-> Do xml-rpc call to client system. $response = $client->call( 'SentToSales', array( $this->valuation->createdBy->email, $this->valuation->sfItemId, $data ) ); $this->logCallback('SentToSales', $packet, is_array($response) ? $response : array($response)); } catch (\Exception $e) { $this->logCallback('SentToSales', $packet, array($e->getMessage()), 'Exception'); \Utility\Debug::errorLog('triggerSentToSales EXCEPTION', "$e"); } } /** * Make another attempt at client callback. * @param string $methodName * @param \Valuation\Entity\XmlRpcCallbackLog $callback */ protected function retryCallback($methodName, $callback) { $this->valuation = $callback->valuation; $this->client = $this->valuation->xmlRpcClient; $client = new \Zend\XmlRpc\Client( $this->client->callbackUrl ); #-> Prepare data. $data = $this->prepareCallbackData($methodName, $this->valuation); $packet = array( 'Email' => $this->valuation->createdBy->email, 'ItemId' => $this->valuation->sfItemId, 'Valuation' => $data ); try { #-> Do xml-rpc call to client system. $response = $client->call( $methodName, array( $this->valuation->createdBy->email, $this->valuation->sfItemId, $data ) ); $callback->response = is_array($response) ? serialize($response) : serialize(array($response)); $callback->status = 'OK'; $callback->attempts = $callback->attempts + 1; $this->em->flush(); } catch (\Exception $e) { #-> Oops, log the exception. $callback->response = serialize(array($e->getMessage())); $callback->attempts = $callback->attempts + 1; $this->em->flush(); \Utility\Debug::errorLog('retryCallback EXCEPTION', "$e"); } } /** * Retry failed callback up to max of 3 attempts. */ public function retryCallbacks() { $this->em = \Utility\Registry::getEntityManager(); $methods = array( 'SentToSales' ); foreach ($methods as $methodName) { for ($i = 5; $i > 0; $i--) { $callbacks = $this->em->getRepository('Valuation\Entity\XmlRpcCallbackLog') ->findBy(array( 'status' => 'Exception', 'attempts' => $i, 'methodName' => $methodName )); foreach ($callbacks as $callback) { $this->retryCallback($methodName, $callback); } } } } /* ------------------------------------------ TEST UTILITY ------------------------------------------ */ /** * @param string $Email * @param string $ItemId * @param string $Valuation * @return string */ public function SentToSales($Email, $ItemId, $Valuation) { \Utility\Debug::errorLog('SentToSales email', $Email); \Utility\Debug::errorLog('SentToSales itemId', $ItemId); \Utility\Debug::errorLog('SentToSales data', $Valuation); return 'Moo'; } }