/** * @param array $requestData * @return ServiceHelperStruct */ protected function buildServiceHelpers(array $requestData) { $profile = $this->profileFactory->loadProfile($requestData); $session = $this->dataFactory->loadSession($requestData); $dbAdapter = $this->dataFactory->createDbAdapter($profile->getType()); $fileReader = $this->createFileReader($profile, $requestData['format']); $fileWriter = $this->fileIOFactory->createFileWriter($requestData['format']); $dataIO = $this->dataFactory->createDataIO($dbAdapter, $session, $this->logger); return new ServiceHelperStruct($profile, $session, $dbAdapter, $fileReader, $fileWriter, $dataIO); }
/** * Custom cronjob for import */ public function cronAction() { /** @var UploadPathProvider $uploadPathProvider */ $uploadPathProvider = $this->get('swag_import_export.upload_path_provider'); $directory = $uploadPathProvider->getPath(UploadPathProvider::CRON_DIR); $allFiles = scandir($directory); $files = array_diff($allFiles, ['.', '..']); $lockerFilename = '__running'; $lockerFileLocation = $directory . $lockerFilename; if (in_array($lockerFilename, $files)) { $file = fopen($lockerFileLocation, "r"); $fileContent = (int) fread($file, filesize($lockerFileLocation)); fclose($file); if ($fileContent > time()) { echo "There is already an import in progress.\n"; return; } else { unlink($lockerFileLocation); } } if ($files === false || count($files) == 0) { echo "No import files are found\n"; return; } //Create empty file to flag cron as running $timeout = time() + 1800; $file = fopen($lockerFileLocation, "w"); fwrite($file, $timeout); fclose($file); $manager = $this->getModelManager(); $profileRepository = $manager->getRepository(Profile::class); foreach ($files as $file) { $fileExtension = strtolower(pathinfo($file, PATHINFO_EXTENSION)); $fileName = strtolower(pathinfo($file, PATHINFO_FILENAME)); if ($fileExtension == 'xml' || $fileExtension == 'csv') { try { $profile = CommandHelper::findProfileByName($file, $profileRepository); if ($profile === false) { $message = SnippetsHelper::getNamespace()->get('cronjob/no_profile', 'No profile found %s'); throw new \Exception(sprintf($message, $fileName)); } $mediaPath = $uploadPathProvider->getRealPath($file, UploadPathProvider::CRON_DIR); } catch (\Exception $e) { echo $e->getMessage() . "\n"; unlink($lockerFileLocation); return; } try { $return = $this->start($profile, $mediaPath, $fileExtension); $profilesMapper = ['articles', 'articlesImages']; //loops the unprocessed data $pathInfo = pathinfo($mediaPath); foreach ($profilesMapper as $profileName) { $tmpFile = $uploadPathProvider->getRealPath($pathInfo['filename'] . '-' . $profileName . '-tmp.csv', UploadPathProvider::CRON_DIR); if (file_exists($tmpFile)) { $outputFile = str_replace('-tmp', '-swag', $tmpFile); rename($tmpFile, $outputFile); $profile = $this->profileFactory->loadHiddenProfile($profileName); $profileEntity = $profile->getEntity(); $this->start($profileEntity, $outputFile, 'csv'); } } $message = $return['data']['position'] . ' ' . $return['data']['adapter'] . " imported successfully \n"; echo $message; unlink($mediaPath); } catch (\Exception $e) { // copy file as broken $brokenFilePath = $uploadPathProvider->getRealPath('broken-' . $file, UploadPathProvider::DIR); copy($mediaPath, $brokenFilePath); echo $e->getMessage() . "\n"; unlink($lockerFileLocation); return; } } } unlink($lockerFileLocation); }