From 662b088daae5cd66237086eec7d9e4f4f269b602 Mon Sep 17 00:00:00 2001 From: Dirk Date: Tue, 13 Aug 2019 11:17:34 +0200 Subject: [PATCH] Refactoring Import Service --- Classes/Command/CleanupCommand.php | 25 +-- .../Command/DestinationDataImportCommand.php | 42 ++-- Classes/Service/CleanupService.php | 184 +----------------- .../Service/DestinationDataImportService.php | 95 ++++----- Configuration/TypoScript/constants.typoscript | 2 +- 5 files changed, 74 insertions(+), 274 deletions(-) diff --git a/Classes/Command/CleanupCommand.php b/Classes/Command/CleanupCommand.php index 12ce358..054fb31 100644 --- a/Classes/Command/CleanupCommand.php +++ b/Classes/Command/CleanupCommand.php @@ -2,26 +2,17 @@ namespace Wrm\Events\Command; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Core\Bootstrap; +use TYPO3\CMS\Extbase\Object\ObjectManager; + +use Wrm\Events\Service\CleanupService; class CleanupCommand extends Command { - protected $restExperience; - protected $storagePid; - protected $regionUid; - protected $categoryParentUid; - protected $filesFolder; - - protected $cliOutput; - protected $cliInput; - - protected $cleanupService; - public function configure() { $this->setDescription('Cleanup Events'); @@ -30,14 +21,10 @@ class CleanupCommand extends Command { protected function execute(InputInterface $input, OutputInterface $output) { - $this->cliOutput = $output; - $this->cliInput = $input; - Bootstrap::initializeBackendAuthentication(); - $this->objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'); - $this->destinationDataImportService = $this->objectManager->get('Wrm\\Events\\Service\\CleanupService'); - - return $this->cleanupService->doClean(); + return GeneralUtility::makeInstance(ObjectManager::class) + ->get(CleanupService::class) + ->doClean(); } } \ No newline at end of file diff --git a/Classes/Command/DestinationDataImportCommand.php b/Classes/Command/DestinationDataImportCommand.php index dfda6dc..734e214 100644 --- a/Classes/Command/DestinationDataImportCommand.php +++ b/Classes/Command/DestinationDataImportCommand.php @@ -8,20 +8,12 @@ use Symfony\Component\Console\Output\OutputInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Core\Bootstrap; +use TYPO3\CMS\Extbase\Object\ObjectManager; + +use Wrm\Events\Service\DestinationDataImportService; class DestinationDataImportCommand extends Command { - protected $restExperience; - protected $storagePid; - protected $regionUid; - protected $categoryParentUid; - protected $filesFolder; - - protected $cliOutput; - protected $cliInput; - - protected $destinationDataImportService; - public function configure() { $this->setDescription('Import Destination Data Events'); @@ -59,26 +51,16 @@ class DestinationDataImportCommand extends Command { protected function execute(InputInterface $input, OutputInterface $output) { - $this->cliOutput = $output; - $this->cliInput = $input; - - $this->storagePid = $input->getArgument('storage-pid'); - $this->regionUid = $input->getArgument('region-uid'); - $this->categoryParentUid = $input->getArgument('category-parent-uid'); - $this->filesFolder = $input->getArgument('files-folder'); - $this->restExperience = $input->getArgument('rest-experience'); - Bootstrap::initializeBackendAuthentication(); - $this->objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'); - $this->destinationDataImportService = $this->objectManager->get('Wrm\\Events\\Service\\DestinationDataImportService'); - - return $this->destinationDataImportService->import( - $this->restExperience, - $this->storagePid, - $this->regionUid, - $this->categoryParentUid, - $this->filesFolder - ); + return GeneralUtility::makeInstance(ObjectManager::class) + ->get(DestinationDataImportService::class) + ->import( + $input->getArgument('rest-experience'), + $input->getArgument('storage-pid'), + $input->getArgument('region-uid'), + $input->getArgument('category-parent-uid'), + $input->getArgument('files-folder') + ); } } \ No newline at end of file diff --git a/Classes/Service/CleanupService.php b/Classes/Service/CleanupService.php index e2798f7..88afad6 100644 --- a/Classes/Service/CleanupService.php +++ b/Classes/Service/CleanupService.php @@ -2,164 +2,23 @@ namespace Wrm\Events\Service; -use TYPO3\CMS\Core\Resource\FileRepository; -use TYPO3\CMS\Core\Resource\Index\MetaDataRepository; -use TYPO3\CMS\Core\Resource\ResourceFactory; use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Core\Database\ConnectionPool; +use TYPO3\CMS\Core\Log\LogManager; use TYPO3\CMS\Extbase\Configuration\ConfigurationManager; use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; -use TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository; -use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager; use TYPO3\CMS\Extbase\Object\ObjectManager; -use TYPO3\CMS\Extbase\Persistence\ObjectStorage; - -use Wrm\Events\Domain\Repository\DateRepository; -use Wrm\Events\Domain\Repository\EventRepository; -use Wrm\Events\Domain\Repository\OrganizerRepository; -use Wrm\Events\Domain\Repository\RegionRepository; class CleanupService { /** - * @var - */ - protected $restUrl; - /** - * @var - */ - protected $restLicenseKey; - /** - * @var - */ - protected $restType; - /** - * @var - */ - protected $restLimit; - /** - * @var - */ - protected $restTemplate; - /** - * @var - */ - protected $restExperience; - /** - * @var - */ - protected $storagePid; - /** - * @var - */ - protected $regionUid; - /** - * @var - */ - protected $categoryParentUid; - /** - * @var - */ - protected $filesFolder; - /** - * @var - */ - protected $storage; - /** - * @var array - */ - protected $settings = []; - /** - * @var bool - */ - protected $tmpCurrentEvent = FALSE; - /** - * @var - */ - protected $logger; - /** - * @var EventRepository - */ - protected $eventRepository; - /** - * @var RegionRepository - */ - protected $regionRepository; - /** - * @var OrganizerRepository - */ - protected $organizerRepository; - /** - * @var DateRepository - */ - protected $dateRepository; - /** - * @var CategoryRepository - */ - protected $sysCategoriesRepository; - /** - * @var FileRepository - */ - protected $fileRepository; - /** - * @var MetaDataRepository - */ - protected $metaDataRepository; - /** - * @var ConfigurationManager - */ - protected $configurationManager; - /** - * @var ObjectManager - */ - protected $objectManager; - /** - * @var PersistenceManager - */ - protected $persistenceManager; - /** - * @var ResourceFactory - */ - protected $resourceFactory; - - /** - * ImportService constructor. - * @param EventRepository $eventRepository - * @param RegionRepository $regionRepository - * @param OrganizerRepository $organizerRepository - * @param DateRepository $dateRepository - * @param CategoryRepository $sysCategoriesRepository - * @param FileRepository $fileRepository - * @param MetaDataRepository $metaDataRepository + * Cleanup Service constructor. * @param ConfigurationManager $configurationManager - * @param PersistenceManager $persistenceManager - * @param ResourceFactory $resourceFactory * @param ObjectManager $objectManager */ public function __construct( - EventRepository $eventRepository, - RegionRepository $regionRepository, - OrganizerRepository $organizerRepository, - DateRepository $dateRepository, - CategoryRepository $sysCategoriesRepository, - FileRepository $fileRepository, - MetaDataRepository $metaDataRepository, ConfigurationManager $configurationManager, - PersistenceManager $persistenceManager, - ResourceFactory $resourceFactory, ObjectManager $objectManager ) { - $this->eventRepository = $eventRepository; - $this->regionRepository = $regionRepository; - $this->organizerRepository = $organizerRepository; - $this->dateRepository = $dateRepository; - $this->sysCategoriesRepository = $sysCategoriesRepository; - $this->fileRepository = $fileRepository; - $this->metaDataRepository = $metaDataRepository; - $this->configurationManager = $configurationManager; - $this->persistenceManager = $persistenceManager; - $this->resourceFactory = $resourceFactory; - $this->objectManager = $objectManager; // Get Typoscript Settings $this->settings = $this->configurationManager->getConfiguration( @@ -168,45 +27,14 @@ class CleanupService { 'Pi1' ); - // Set properties - $this->restUrl = $this->settings['destinationData']['restUrl']; - $this->restLicenseKey = $this->settings['destinationData']['license']; - $this->restType = $this->settings['destinationData']['restType']; - $this->restLimit = $this->settings['destinationData']['restLimit']; - $this->restTemplate = $this->settings['destinationData']['dataTemplate']; - // Init Logger - // Nötig, damit logger arbeitet? - $GLOBALS['TYPO3_CONF_VARS']['LOG']['writerConfiguration'] = [ - \TYPO3\CMS\Core\Log\LogLevel::INFO => [ - 'TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter' => [ - 'logFile' => 'typo3temp/logs/events_cleanup' - ] - ] - ]; - - $this->logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class)->getLogger(__CLASS__); - $this->logger->info('Starting Destination Data Import Service'); + $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__); + $this->logger->info('Event Cleanup Service'); } - /** - * @param $restExperience - * @param $storagePid - * @param $regionUid - * @param $categoryParentUid - * @param $filesFolder - */ public function doCleanup() { - + // To be done + // Hmpf } - - /** - * @param $data - * @return int - */ - public function processData($data) { - - } - } \ No newline at end of file diff --git a/Classes/Service/DestinationDataImportService.php b/Classes/Service/DestinationDataImportService.php index 7ba84d0..0560ad0 100644 --- a/Classes/Service/DestinationDataImportService.php +++ b/Classes/Service/DestinationDataImportService.php @@ -9,6 +9,7 @@ use TYPO3\CMS\Core\Resource\ResourceFactory; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\DataHandling\SlugHelper; use TYPO3\CMS\Core\Database\ConnectionPool; +use TYPO3\CMS\Core\Log\LogManager; use TYPO3\CMS\Extbase\Configuration\ConfigurationManager; use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; @@ -22,6 +23,7 @@ use Wrm\Events\Domain\Repository\EventRepository; use Wrm\Events\Domain\Repository\OrganizerRepository; use Wrm\Events\Domain\Repository\RegionRepository; + class DestinationDataImportService { /** @@ -221,7 +223,7 @@ class DestinationDataImportService { // Set Configuration $this->configurationManager->setConfiguration(array_merge($frameworkConfiguration, $persistenceConfiguration)); - $this->logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class)->getLogger(__CLASS__); + $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__); $this->logger->info('Starting Destination Data Import Service'); $restUrl = $this->restUrl . '?experience=' . $this->restExperience . '&licensekey=' . $this->restLicenseKey . '&type=' . $this->restType . '&limit=' . $this->restLimit . '&template=' . $this->restTemplate; @@ -288,10 +290,9 @@ class DestinationDataImportService { $sysParentCategory = $this->sysCategoriesRepository->findByUid($this->categoryParentUid); foreach ($categories as $categoryTitle) { $tmpSysCategory = $this->sysCategoriesRepository->findOneByTitle($categoryTitle); - if (!$tmpSysCategory) - { + if (!$tmpSysCategory) { $this->logger->info('Creating new category: ' . $categoryTitle); - $tmpSysCategory = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Domain\\Model\\Category'); + $tmpSysCategory = $this->objectManager->get(\TYPO3\CMS\Extbase\Domain\Model\Category::class); $tmpSysCategory->setTitle($categoryTitle); $tmpSysCategory->setParent($sysParentCategory); $tmpSysCategory->setPid($this->sysCategoriesPid); @@ -305,6 +306,7 @@ class DestinationDataImportService { /** * @param array $timeIntervals + * @TODO: split into functions */ protected function setDates(Array $timeIntervals) { @@ -332,7 +334,7 @@ class DestinationDataImportService { if (strtotime($date['start']) > $now) { $this->logger->info('Setup single date'); - $dateObj = $this->objectManager->get('Wrm\\Events\\Domain\\Model\\Date'); + $dateObj = $this->objectManager->get(\Wrm\Events\Domain\Model\Date::class); $start = new \DateTime($date['start'], new \DateTimeZone($date['tz'])); $end = new \DateTime($date['end'], new \DateTimeZone($date['tz'])); $this->logger->info('Start transformed ' . $start->format('Y-m-d H:i')); @@ -360,7 +362,7 @@ class DestinationDataImportService { $eventEnd = new \DateTime(); $eventEnd->setTimestamp($i); $eventEnd->setTime($until->format('H'), $until->format('i')); - $dateObj = $this->objectManager->get('Wrm\\Events\\Domain\\Model\\Date'); + $dateObj = $this->objectManager->get(\Wrm\Events\Domain\Model\Date::class); $dateObj->setStart($eventStart); $dateObj->setEnd($eventEnd); $this->tmpCurrentEvent->addDate($dateObj); @@ -372,7 +374,6 @@ class DestinationDataImportService { else if ($date['freq'] == 'Weekly' && !empty($date['weekdays'])) { foreach ($date['weekdays'] as $day) { - $this->logger->info('Setup weekly interval dates for ' . $day); $this->logger->info('Start ' . $date['start']); $this->logger->info('End ' . $date['repeatUntil']); @@ -387,7 +388,7 @@ class DestinationDataImportService { $eventEnd = new \DateTime(); $eventEnd->setTimestamp($i); $eventEnd->setTime($until->format('H'), $until->format('i')); - $dateObj = $this->objectManager->get('Wrm\\Events\\Domain\\Model\\Date'); + $dateObj = $this->objectManager->get(\Wrm\Events\Domain\Model\Date::class); $dateObj->setStart($eventStart); $dateObj->setEnd($eventEnd); $this->tmpCurrentEvent->addDate($dateObj); @@ -410,25 +411,25 @@ class DestinationDataImportService { { if ($address['rel'] == "organizer") { $tmpOrganizer = $this->organizerRepository->findOneByName($address['name']); - if (!$tmpOrganizer) - { - $tmpOrganizer = $this->objectManager->get('Wrm\\Events\\Domain\\Model\\Organizer'); - - $tmpOrganizer->setName($address['name']); - $tmpOrganizer->setCity($address['city']); - $tmpOrganizer->setZip($address['zip']); - $tmpOrganizer->setStreet($address['street']); - $tmpOrganizer->setPhone($address['phone']); - $tmpOrganizer->setWeb($address['web']); - $tmpOrganizer->setEmail($address['email']); - $tmpOrganizer->setDistrict($address['district']); - - $this->organizerRepository->add($tmpOrganizer); - $this->tmpCurrentEvent->setOrganizer($tmpOrganizer); - - } else { + if ($tmpOrganizer) { $this->tmpCurrentEvent->setOrganizer($tmpOrganizer); + continue; } + + $tmpOrganizer = $this->objectManager->get(\Wrm\Events\Domain\Model\Organizer::class); + + $tmpOrganizer->setName($address['name']); + $tmpOrganizer->setCity($address['city']); + $tmpOrganizer->setZip($address['zip']); + $tmpOrganizer->setStreet($address['street']); + $tmpOrganizer->setPhone($address['phone']); + $tmpOrganizer->setWeb($address['web']); + $tmpOrganizer->setEmail($address['email']); + $tmpOrganizer->setDistrict($address['district']); + + $this->organizerRepository->add($tmpOrganizer); + $this->tmpCurrentEvent->setOrganizer($tmpOrganizer); + } } } @@ -475,24 +476,22 @@ class DestinationDataImportService { $event = $this->eventRepository->findOneByGlobalId($globalId); - if (!$event) - { - $this->logger->info(substr($title, 0, 20) . ' does not exist'); - $event = $this->objectManager->get('Wrm\\Events\\Domain\\Model\\Event'); - - // Create event and persist - $event->setGlobalId($globalId); - $event->setCategories(new ObjectStorage()); - $this->eventRepository->add($event); - $this->persistenceManager->persistAll(); - - $this->logger->info('Not found "' . substr($title, 0, 20) . '..." with global id ' . $globalId . ' in database. Created new one.'); - } else { + if ($event) { // Global ID is found and events gets updated $event = $this->eventRepository->findOneByGlobalId($globalId); $this->logger->info('Found "' . substr($title, 0, 20) . '..." with global id ' . $globalId . ' in database'); + return $event; } + // New event is created + $this->logger->info(substr($title, 0, 20) . ' does not exist'); + $event = $this->objectManager->get(\Wrm\Events\Domain\Model\Event::class); + // Create event and persist + $event->setGlobalId($globalId); + $event->setCategories(new ObjectStorage()); + $this->eventRepository->add($event); + $this->persistenceManager->persistAll(); + $this->logger->info('Not found "' . substr($title, 0, 20) . '..." with global id ' . $globalId . ' in database. Created new one.'); return $event; } @@ -500,6 +499,9 @@ class DestinationDataImportService { * @param array $assets */ protected function setAssets(Array $assets) { + + $error = false; + foreach ($assets as $media_object) { if($media_object['rel'] == "default" && $media_object['type'] == "image/jpeg") { @@ -534,19 +536,20 @@ class DestinationDataImportService { } } + $error = false; } } /** * Load File * @param string $file - * @return bool + * @return string */ protected function loadFile($file) { $directory = $this->environment->getPublicPath() . "/uploads/tx_events/"; $filename = basename($file); $this->logger->info('Getting file ' . $file . ' as ' . $filename); - $asset = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($file); + $asset = GeneralUtility::getUrl($file); if ($asset) { file_put_contents($directory . $filename, $asset); return $filename; @@ -583,18 +586,18 @@ class DestinationDataImportService { $fieldname => $newId ); - $dataHandler = $this->objectManager->get('TYPO3\\CMS\\Core\\DataHandling\\DataHandler'); + $dataHandler = $this->objectManager->get(\TYPO3\CMS\Core\DataHandling\DataHandler::class); $dataHandler->start($data, array()); $dataHandler->process_datamap(); if (count($dataHandler->errorLog) === 0) { return true; - } else { - foreach($dataHandler->errorLog as $error) { - $this->logger->info($error); - } - return false; } + + foreach($dataHandler->errorLog as $error) { + $this->logger->info($error); + } + return false; } /** diff --git a/Configuration/TypoScript/constants.typoscript b/Configuration/TypoScript/constants.typoscript index c6ea1d3..bdcf0ff 100644 --- a/Configuration/TypoScript/constants.typoscript +++ b/Configuration/TypoScript/constants.typoscript @@ -24,7 +24,7 @@ plugin.tx_events { dataLimit = 200 # cat=plugin.tx_events//a; type=string; label=Data Template dataTemplate = ET2014A.json - # cat.plugin.tx_events//a; type=string: Label=Category Storage + # cat=plugin.tx_events//a; type=string; Label=Category Storage categoriesPid = 54 } }