<?php
 namespace Modules\Orders\ImportedOrders\Webservice\Model\ExecOperation; class ImportOrder extends GeneralAbstract { private $fileContentDecoded = NULL; private $importedOrder = NULL; protected function checkRequest() { if (!$this->request->fileContent || !$this->request->fileSize || !$this->request->fileContentChecksum) { throw new \RuntimeException('File content, file size and file content checksum are mandatory.'); } $fileContentDecoded = base64_decode($this->request->fileContent); if ($fileContentDecoded === FALSE) { throw new \RuntimeException('The file content is not base64_encoded.'); } $this->fileContentDecoded = $fileContentDecoded; $fileContentXML = @simplexml_load_string($fileContentDecoded, '\Engine\SimpleXmlElement'); if (!$fileContentXML) { throw new \RuntimeException('The file content is not XML valid.'); } if (strlen($fileContentDecoded) != $this->request->fileSize) { throw new \RuntimeException("The size of the decoded file content is not the same with the one in the request."); } if (crc32($fileContentDecoded) != $this->request->fileContentChecksum) { throw new \RuntimeException("Invalid checksum."); } try { $importedOrderParser = new \App\ImportedOrder\Parser\Xml_V20($fileContentXML); $importedOrderParser->createImportedOrder(); $importedOrder = $importedOrderParser->getImportedOrder(); $this->importedOrder = $importedOrder; } catch (\App\MessagesException $ex) { throw new \RuntimeException(sprintf("Invalid imported order. Error message: %s.", $ex->getMessagesCollector()->getMessageBulk())); } $idExternalSystem = \Entity\ExtResSystem::getSystemIdByCode($this->importedOrder->externalSystemCode); if (!$idExternalSystem) { throw new \RuntimeException("Invalid external system code."); } $extResSystemEntity = \Entity\ExtResSystem::select($idExternalSystem); if ($extResSystemEntity->username != $this->request->username) { throw new \RuntimeException("Username is not associated with the external system code from request."); } } protected function executeRequestedOperation() { try { \DB::startTransaction(); $importedOrderEntity = \Entity\ImportedOrder::insert(); $importedOrderEntity->order = $this->importedOrder; $importedOrderEntity->originalFilesContent = [ [ 'content' => $this->fileContentDecoded, 'name' => 'importedOrder.xml' ] ]; $importedOrderEntity->save(); \DB::commitTransaction(); $this->response->idImportedOrder = $importedOrderEntity->getId(); } catch (\App\Entity\Exception $ex) { \DB::rollbackTransaction(); throw new \RuntimeException(sprintf("Cannot import order. Error message: %s.", $ex->getMessageBulk())); } } }