Improve timetamp parsing, update date of last import in backend

This commit is contained in:
Björn Fromme
2021-02-04 21:14:51 +01:00
parent c8964e4ec6
commit 407e625aa9
3 changed files with 32 additions and 22 deletions
@@ -29,6 +29,7 @@ namespace EP\EpProducts\Controller;
use EP\EpProducts\Service\ContingentImportService; use EP\EpProducts\Service\ContingentImportService;
use EP\EpProducts\Service\DateImportService; use EP\EpProducts\Service\DateImportService;
use EP\EpProducts\Service\ImportTimestampService;
use EP\EpProducts\Service\LogService; use EP\EpProducts\Service\LogService;
use EP\EpProducts\Service\SnowreportImportService; use EP\EpProducts\Service\SnowreportImportService;
use TYPO3\CMS\Backend\View\BackendTemplateView; use TYPO3\CMS\Backend\View\BackendTemplateView;
@@ -67,6 +68,10 @@ class BackendController extends ActionController
* @var ContingentImportService * @var ContingentImportService
*/ */
protected $contingentImportService; protected $contingentImportService;
/**
* @var ImportTimestampService
*/
private $importTimestampService;
/** /**
* @param DateImportService $dateImportService * @param DateImportService $dateImportService
@@ -79,7 +84,8 @@ class BackendController extends ActionController
DateImportService $dateImportService, DateImportService $dateImportService,
ContingentImportService $contingentImportService, ContingentImportService $contingentImportService,
SnowreportImportService $snowreportService, SnowreportImportService $snowreportService,
LogService $logService LogService $logService,
ImportTimestampService $importTimestampService
) )
{ {
parent::__construct(); parent::__construct();
@@ -88,14 +94,12 @@ class BackendController extends ActionController
$this->contingentImportService = $contingentImportService; $this->contingentImportService = $contingentImportService;
$this->snowreportImportService = $snowreportService; $this->snowreportImportService = $snowreportService;
$this->logService = $logService; $this->logService = $logService;
$this->importTimestampService = $importTimestampService;
} }
public function indexAction() public function indexAction()
{ {
$updatedTimestampPath = GeneralUtility::getFileAbsFileName('fileadmin/xmlexport/.lastupdate'); $updatedTimestamp = $this->importTimestampService->readImportTimestamp('fileadmin/products_import.info');
if ($updatedTimestampPath && file_exists($updatedTimestampPath)) {
$updatedTimestamp = file_get_contents($updatedTimestampPath);
}
$this->view->assign('updatedTimestamp', $updatedTimestamp); $this->view->assign('updatedTimestamp', $updatedTimestamp);
} }
@@ -6,11 +6,29 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
class ImportTimestampService class ImportTimestampService
{ {
public function readImportTimestamp(string $importTimestampFile): ?\DateTime
{
$lastImportTimestampPath = GeneralUtility::getFileAbsFileName($importTimestampFile);
if (!$lastImportTimestampPath || !file_exists($lastImportTimestampPath)) {
return null;
}
$line = trim(file($lastImportTimestampPath)[0]);
return \DateTime::createFromFormat('d.m.Y H:i:s', $line);
}
public function writeImportTimestamp(string $importTimestampFile): void
{
$lastImportTimestampPath = GeneralUtility::getFileAbsFileName($importTimestampFile);
$now = new \DateTime('now');
file_put_contents($lastImportTimestampPath, $now->format('d.m.Y H:i:s'));
}
public function isImportRequired(string $uploadTimestampFile, string $importTimestampFile): bool public function isImportRequired(string $uploadTimestampFile, string $importTimestampFile): bool
{ {
$updatedTimestampPath = GeneralUtility::getFileAbsFileName($uploadTimestampFile); $updatedTimestampPath = GeneralUtility::getFileAbsFileName($uploadTimestampFile);
// No upload yet so no import required
if (!$updatedTimestampPath || !file_exists($updatedTimestampPath)) { if (!$updatedTimestampPath || !file_exists($updatedTimestampPath)) {
return false; return false;
} }
@@ -18,22 +36,10 @@ class ImportTimestampService
$line = trim(file($updatedTimestampPath)[0]); $line = trim(file($updatedTimestampPath)[0]);
$lastUploadAt = \DateTime::createFromFormat('d.m.Y H:i:s', $line); $lastUploadAt = \DateTime::createFromFormat('d.m.Y H:i:s', $line);
$lastImportTimestampPath = GeneralUtility::getFileAbsFileName($importTimestampFile); $lastImportAt = $this->readImportTimestamp($importTimestampFile);
// No import yet so import required if (null === $lastImportAt || $lastUploadAt > $lastImportAt) {
if (!$lastImportTimestampPath || !file_exists($lastImportTimestampPath)) { $this->writeImportTimestamp($importTimestampFile);
$now = new \DateTime('now');
file_put_contents($lastImportTimestampPath, $now->format('d.m.Y H:i:s'));
return true;
}
$line = trim(file($lastImportTimestampPath)[0]);
$lastImportAt = \DateTime::createFromFormat('d.m.Y H:i:s', $line);
if ($lastUploadAt > $lastImportAt) {
$now = new \DateTime('now');
file_put_contents($lastImportTimestampPath, $now->format('d.m.Y H:i:s'));
return true; return true;
} }
@@ -8,7 +8,7 @@
<h1>E&amp;P Funktionen</h1> <h1>E&amp;P Funktionen</h1>
<f:flashMessages /> <f:flashMessages />
<p> <p>
<strong>Letzter XML Abruf aus BusPro: {f:if(condition: updatedTimestamp, then: updatedTimestamp, else: '-')}</strong> <strong>Letzter automatischer XML Import: {f:if(condition: updatedTimestamp, then: '{updatedTimestamp -> f:format.date(format: \'d.m.Y H:i:s\')}', else: '-')}</strong>
</p> </p>
<p> <p>
<f:link.action action="importProducts" class="btn btn-default"> <f:link.action action="importProducts" class="btn btn-default">