addRequiredInput( array( 'Upload' => array( 'csvFile' => 'Id' ) ) ); return new ContractUseOnce( new UseCaseOptions(), $requirement ); } /** * Upload and run the TU data import. * @param object|null $jobRecord * @param \Workspace\Utility\ServiceInputParams $contract * @return array */ public function executeUpload($jobRecord, ServiceInputParams $contract) { $jobRecord = null; $uploadId = $contract->data->Upload['csvFile']; $document = $this->em->find('Utility\\Entity\\Document', $uploadId); if (is_null($document)) { Debug::errorLog( 'TU Data Import EXCEPTION', 'Could not find document id: ' . $uploadId ); return; } $fileName = $document->filename; // $this->util = new ImportUtility($this->em); // $this->store = new DataStore(); // $this->helper = new ImportHelper($this->em, $this->store); // // $this->helper->executeOfflineTransunionDataImport($fileName); $this->offlineTransunionDataImport($fileName); return $contract->success('Import being processed.', array()); } /** * @param $fileName */ protected function offlineTransunionDataImport($fileName) { exec( 'php /var/www/B4C2/public/index.php vehicle import ' . $fileName . ' >>/log/php.log &' ); } #-------------------------------------------------- OFFLINE PROCESSING /** * CRON functionality: Import updated transunion vehicle data. * @param string $filename */ public function scriptUpdateVehicleData($filename) { #-> Ensure we don't have problem with script timeout. set_time_limit(0); #-> Prepare helpers. $this->util = new ImportUtility($this->em); $this->store = new DataStore(); $this->helper = new ImportHelper($this->em, $this->store); #-> Catch errors. try { #-> Get things ready. $this->prepareForCsvProcessing($filename); #-> Handle csv data. while (($data = $this->importer->getRecord(ImportCsv::FETCH_ASSOC)) !== false) { $this->processCsvEntryForPersistenceIfRequired($data); } #-> Cleanup. $this->cleanupAfterCsvProcessing(); } catch (\Exception $e) { Debug::errorLog('TU Data Import EXCEPTION', $e->getMessage()); Debug::errorLog('TU Data Import TRACE', $e->getTraceAsString()); } } /** * @param $filename */ protected function prepareForCsvProcessing($filename) { #-> Establish new synchronization version to use. $this->store->version = $this->util->getLatestSynchVersion( 'Stock\\Entity\\Type' ) + 1; #-> Ensure that synchronization versions have a full sequence. $this->helper->ensureSynchronizationVersionSequence(); #-> Open uploaded csv file. $this->importer = new ImportCsv( getcwd() . '/public/documents/' . $filename, true ); #-> Prepare common variables. $this->previousMmCode = false; $this->em->beginTransaction(); } /** * Process a csv line entry. * @param $packet */ protected function processCsvEntryForPersistenceIfRequired($packet) { #-> Ensure null instead of empty string on numeric fields. $packet = $this->helper->cleanupNumericInputFields($packet); #-> Only handle an mm-code once. if ($this->previousMmCode == $packet['MMCode']) { return; } $this->previousMmCode = $packet['MMCode']; #-> Safety check for year range. $introYear = explode('/', $packet['IntroDate'])[1]; if ($introYear < 1970) { return; } #-> Handle vehicle make. $makeId = $this->helper->createOrUpdateVehicleMakeEntry($packet); #-> Handle vehicle model. $modelId = $this->helper->createOrUpdateVehicleModel($packet, $makeId); #-> Handle vehicle type. $this->helper->createOrUpdateVehicleType($packet, $modelId); #-> Bump the version? if ($this->store->numItems >= 200) { $this->store->version = $this->helper->processBatch(); } } /** */ protected function cleanupAfterCsvProcessing() { $this->em->flush(); $this->em->commit(); $this->importer = null; } }