<?php
namespace Modules\Orders\Amadeus\FluX; class Controller extends \ModulesAbstract\FluxRest\ControllerUploaderAutoSaveFile { protected function getBasePath() { global $CFG; return $CFG['paths']['amadeus']; } protected function validateFluXSystem(\Entity\FluxInstance $fluxInstance) { return ($fluxInstance->assignedSystem=='amadeus'); } protected function handleSystemHealth($request, \ModulesAbstract\FluxRest\Response\Main $mainResponse, \Entity\FluxInstance $fluxInstance) { parent::handleSystemHealth($request, $mainResponse, $fluxInstance); if (!$mainResponse->hasOperation('Wait')) { $behaviourOnSystemLoad = []; foreach (explode(",", $fluxInstance->behaviourOnSystemLoad) as $behaviour) { list($k, $v) = explode(":", $behaviour); $behaviourOnSystemLoad[$k] = $v; } global $DBTImportedOrders_services, $DBTImportedOrders_tickets, $DBTImportedOrders_orders, $DBTAmadeusParsedFiles, $DBTAmadeusServices; $openTables = \DB::getOpenTables(); $busyTables = array_intersect([$DBTImportedOrders_services, $DBTImportedOrders_tickets, $DBTImportedOrders_orders, $DBTAmadeusParsedFiles, $DBTAmadeusServices, ], $openTables); if ($busyTables) { $op = new \ModulesAbstract\FluxRest\Response\Operation\NotifyUserLog(); $op->text = sprintf('Server busy, some database tables are in use (%s)', implode(", ", $busyTables)); $mainResponse->appendOperation($op); $op = new \ModulesAbstract\FluxRest\Response\Operation\Wait(); $op->timeout = count($busyTables)*2.5; $mainResponse->appendOperation($op); if ($behaviourOnSystemLoad['onTablesBusy']=='wait') { throw new \Exception\IO\Base("System busy"); } } } } protected function parseFileOnDisk($path, $originalFilename, \Entity\FluxInstance $fluxInstance, $rawRequest) { \DB::startTransaction(); $transactionTimings = [ 'start' => null, 'FileParser' => null, 'Storage' => null, 'CompatibilityWithImportedOrders' => null, 'commit' => null, ]; try { set_time_limit(600); $transactionTimings['start'] = microtime(true); $parser = new \App\Amadeus\FileParser\ParsedFile($path); $parser->parse(); $parser->getStructure()->getStructure(); $parser->getStats(); $transactionTimings['FileParser'] = microtime(true); $structureParser = new \App\Amadeus\StructureParser\Processor(); $structureParser->setFile($path); $structureParser->setParser($parser); $storage = new \App\Amadeus\Storage\StorageHandler(); $storage->setProcessor($structureParser); $storage->setOption('source', 'FluX'); $storage->setOption('IP', ($_SERVER['REMOTE_ADDR']?:'')); $ret = $storage->execute(); $transactionTimings['Storage'] = microtime(true); if ($ret) { $CFG = \Engine\Loader_Cfg::getInstance()->getGlobal(); $convertor = new $CFG['amadeus']['CompatibilityWithImportedOrders']['Processor']['class']([]); $convertor->importFromStorageResult($ret); $order = $convertor->getConvertedOrder(); $orders = $convertor->splitOrderIfNeeded($order); $insertedOrderIds = []; foreach ($orders as $order) { $orderInsert = \Entity\ImportedOrder::insert(); $orderInsert->order = $order; $orderInsert->originalFilesWithPath = [$this->getFilePathRelativeToFilesRoot($path)]; $orderInsert->save(); $insertedOrderIds[]= $orderInsert->getId(); } $affectedFiles = []; foreach ($convertor->getServices() as $ent) { $affectedFiles[$ent->idFile] = $ent->file; } foreach ($affectedFiles as $file) { $fileUpd = \Entity\AmadeusFile::update($file->getId()); $idImportedOrders = $file->idImportedOrders; foreach ($insertedOrderIds as $insertedOrderId) { $idImportedOrders[] = $insertedOrderId; } $fileUpd->idImportedOrders = $idImportedOrders; $fileUpd->save(); } $transactionTimings['CompatibilityWithImportedOrders'] = microtime(true); } $parser->destroyCCInformation(); \DB::commitTransaction(); $transactionTimings['commit'] = microtime(true); $this->queueSessionData[__FUNCTION__] = $transactionTimings; } catch(\Exception $ex) { \DB::rollbackTransaction(); \App\Logger\FluxInstance::getInstance()->warning(sprintf("[%s] %s: %s", __METHOD__, get_class($ex), $ex->getMessage()), [ 'file' => new \Engine\Logger\Context\File([ 'name'=> $originalFilename, 'content' => \Engine\File::getContents($path), ]), 'exception' => new \Engine\Logger\Context\ExceptionAsHtml($ex), 'fluxInstance' => $fluxInstance, ]); $hiddenExceptions = [ 'App\Amadeus\FileParser\ParserException', 'App\Amadeus\StructureParser\StructureException', 'App\Amadeus\Storage\Exception', 'App\Amadeus\Storage\DuplicateTicketException', 'App\Amadeus\Exception', 'App\Utils\BaseParser\Structure\StructureParser\Types\Exception\Exception', ]; if (in_array(get_class($ex), $hiddenExceptions)) { throw new \ModulesAbstract\FluxRest\ExceptionInvalidFile(sprintf("Invalid file %s. Error: %s", $originalFilename, $ex->getMessage()), null, $ex); } else { throw $ex; } } } } 