params = $params; $this->messageBus = $messageBus; $this->directory = $directory; $this->logService = $logService; } /** * @throws InternalServerErrorMaestroException */ public function __invoke(GenerationExecuteMessage $message) { $this->logService->setLog('GenerationExecuteHandler : Entry-point.'); // ini_set('default_socket_timeout', 600); // USE THIS AS A DEBUG WHEN GENERATION NOT RETURNED IN DB AND "/var/log/execute.log" as "Error, can't execute generation : Error Fetching http headers" (cause makeSoapRequest() falls in catch) try { $absoluteInPath = $message->getGenerationNotificationMessage()->getGenerationInLocation(); $scriptArgs = json_decode($message->getGeneratedScriptArgs(), true); $result = $this->makeSoapRequest($this->params->get('indesign_port_0'), $scriptArgs); $this->logService->setLog('InDesign returned.'); $this->logService->setLog($absoluteInPath . ' : ' . json_encode($result)); if (is_array($result) /*&& isset($result['errorNumber']) && 0 !== $result['errorNumber']*/) { $error = $result['errorString'] ?? "An error occured but there is no 'errorString' param to return."; $this->unrecoverableError($error, $absoluteInPath); } else if ($result instanceof SoapFault) { $error = "Error, can't execute generation, unexpected result. Received SoapFault. " . $result->getMessage(); $this->unrecoverableError($error, $absoluteInPath); } else { if (property_exists($result, 'scriptResult')) { if (property_exists($result->scriptResult, 'data')) { $scriptResultData = json_decode($result->scriptResult->data, true); if (isset($scriptResultData['error'])) { if (0 === $scriptResultData['error']) { $this->messageBus->dispatch($message->getGenerationNotificationMessage(), [new AmqpStamp('notify_generation')]); } else { $ret = isset($scriptResultData['retour']) ? reset($scriptResultData['retour']) : "No information's return provided !"; $error = "Error : " . $scriptResultData['error'] . ". " . $ret; $this->unrecoverableError($error, $absoluteInPath); } } else { $error = "Error, property 'error' not found"; $this->unrecoverableError($error, $absoluteInPath); } } else { $error = "Error, property 'data' in 'scriptResult' not found !"; $this->unrecoverableError($error, $absoluteInPath); } } else { $error = "Error, property 'scriptResult' not found !"; $this->unrecoverableError($error, $absoluteInPath); } } } catch (Exception $e) { $error = "Error, can't execute generation : " . $e->getMessage(); $this->unrecoverableError($error, $absoluteInPath); } } /** * @param string $indesignPort * @param array $scriptArgs * * @return array|SoapFault|stdClass * @throws SoapFault */ private function makeSoapRequest(string $indesignPort, array $scriptArgs) { $host = 'http://' . $this->params->get('indesign_server') . ":" . $indesignPort; $wsdl = $host . '/Service?wsdl'; $soapClient = new SoapClient($wsdl, array( 'encoding' => 'utf-8', 'location' => $host, 'trace' => true, 'keep_alive' => true, 'connection_timeout' => 5000, 'cache_wsdl' => WSDL_CACHE_NONE, 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE, )); // $result = $soapClient->__soapCall('RunScript', $scriptArgs); return $soapClient->RunScript($scriptArgs); } /** * @param string $error * @param string $absoluteInPath * * @return void * @throws InternalServerErrorMaestroException */ private function unrecoverableError(string $error, string $absoluteInPath): void { if (!$this->params->get('generation_keep_files')) { $deleted = $this->directory->removeFromPath($absoluteInPath); if ($deleted instanceof Exception) { $error .= $deleted->getMessage(); } } $this->logService->setLog($absoluteInPath . ' : ' . $error, 'execute.log'); throw new UnrecoverableMessageHandlingException($error); } }