-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from aligent/feature/export_improvements
Feature/export improvements
- Loading branch information
Showing
12 changed files
with
287 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
namespace Aligent\FredhopperExport\Controller\Adminhtml\Exports; | ||
|
||
use Aligent\FredhopperExport\Api\Data\ExportInterface; | ||
use Aligent\FredhopperExport\Model\Data\Export; | ||
use Aligent\FredhopperExport\Model\Data\ExportFactory; | ||
use Aligent\FredhopperExport\Model\ResourceModel\Data\Export as ExportResource; | ||
use Magento\Backend\App\Action; | ||
use Magento\Backend\App\Action\Context; | ||
use Magento\Framework\App\Filesystem\DirectoryList; | ||
use Magento\Framework\App\Response\Http\FileFactory; | ||
use Magento\Framework\App\ResponseInterface; | ||
use Magento\Framework\Filesystem\Io\File as Filesystem; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class Data extends Action | ||
{ | ||
|
||
public const string ADMIN_RESOURCE = 'Aligent_FredhopperExport::manage'; | ||
|
||
/** | ||
* @param Context $context | ||
* @param ExportFactory $exportFactory | ||
* @param ExportResource $exportResource | ||
* @param FileFactory $fileFactory | ||
* @param LoggerInterface $logger | ||
* @param Filesystem $filesystem | ||
*/ | ||
public function __construct( | ||
readonly Context $context, | ||
private readonly ExportFactory $exportFactory, | ||
private readonly ExportResource $exportResource, | ||
private readonly FileFactory $fileFactory, | ||
private readonly LoggerInterface $logger, | ||
private readonly Filesystem $filesystem | ||
) { | ||
parent::__construct($context); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute(): ResponseInterface | ||
{ | ||
$exportId = (int)$this->getRequest()->getParam('id'); | ||
if (empty($exportId)) { | ||
$this->getMessageManager()->addErrorMessage(__('Export ID is required.')); | ||
$this->_redirect('*/*/index'); | ||
return $this->_response; | ||
} | ||
$isZip = (bool)$this->getRequest()->getParam('zip'); | ||
/** @var Export $export */ | ||
$export = $this->exportFactory->create(); | ||
$this->exportResource->load($export, $exportId); | ||
if ($export->isEmpty()) { | ||
$this->getMessageManager()->addErrorMessage(__('Could not load export data.')); | ||
$this->_redirect('*/*/index'); | ||
return $this->_response; | ||
} | ||
$directory = $export->getDirectory(); | ||
$filename = match ($export->getExportType()) { | ||
ExportInterface::EXPORT_TYPE_INCREMENTAL => ExportInterface::ZIP_FILENAME_INCREMENTAL, | ||
ExportInterface::EXPORT_TYPE_FULL => ExportInterface::ZIP_FILENAME_FULL, | ||
ExportInterface::EXPORT_TYPE_SUGGEST => ExportInterface::ZIP_FILENAME_SUGGEST, | ||
default => '' | ||
}; | ||
if (empty($filename)) { | ||
$this->getMessageManager()->addErrorMessage(__('Could not download data file.')); | ||
$this->_redirect('*/*/index'); | ||
return $this->_response; | ||
} | ||
|
||
$filename = $directory . DIRECTORY_SEPARATOR . $filename; | ||
$name = $this->filesystem->getPathInfo($filename)['basename'] ?? $filename; | ||
try { | ||
return $this->fileFactory->create( | ||
$name, | ||
[ | ||
'type' => 'filename', | ||
'value' => $filename, | ||
'rm' => false | ||
], | ||
DirectoryList::VAR_DIR, | ||
'application/zip' | ||
); | ||
} catch (\Exception $e) { | ||
$this->logger->error($e->getMessage(), ['exception' => $e]); | ||
$this->getMessageManager()->addErrorMessage(__('Could not download data file.')); | ||
$this->_redirect('*/*/index'); | ||
return $this->_response; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
namespace Aligent\FredhopperExport\Model; | ||
|
||
use Aligent\FredhopperExport\Api\Data\ExportInterface; | ||
use Aligent\FredhopperExport\Model\ResourceModel\Data\Export\Collection; | ||
use Aligent\FredhopperExport\Model\ResourceModel\Data\Export\CollectionFactory; | ||
|
||
class GetExportIsInProgress | ||
{ | ||
private const array IN_PROGRESS_STATUSES = [ | ||
ExportInterface::STATUS_UPLOADED, | ||
ExportInterface::STATUS_TRIGGERED | ||
]; | ||
|
||
/** | ||
* @param CollectionFactory $collectionFactory | ||
*/ | ||
public function __construct( | ||
private readonly CollectionFactory $collectionFactory | ||
) { | ||
} | ||
|
||
/** | ||
* Check if there is already an export in uploaded/triggered status. | ||
* | ||
* If $triggeredOnly is true, only check for triggered status | ||
* | ||
* @param bool $triggeredOnly | ||
* @return bool | ||
*/ | ||
public function execute(bool $triggeredOnly = false): bool | ||
{ | ||
/** @var Collection $collection */ | ||
$collection = $this->collectionFactory->create(); | ||
$statusesToCheck = $triggeredOnly ? [ExportInterface::STATUS_TRIGGERED] : self::IN_PROGRESS_STATUSES; | ||
$collection->addFieldToFilter('status', ['in' => self::IN_PROGRESS_STATUSES]); | ||
return $collection->getSize() > 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
namespace Aligent\FredhopperExport\Model; | ||
|
||
use Aligent\FredhopperExport\Api\Data\ExportInterface; | ||
use Aligent\FredhopperExport\Model\Data\Export; | ||
use Aligent\FredhopperExport\Model\Data\GetCurrentExportedVersion; | ||
use Aligent\FredhopperExport\Model\ResourceModel\Data\Export as ExportResource; | ||
use Aligent\FredhopperExport\Model\ResourceModel\Data\Export\Collection; | ||
use Aligent\FredhopperExport\Model\ResourceModel\Data\Export\CollectionFactory; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class InvalidateExports | ||
{ | ||
/** | ||
* @param CollectionFactory $collectionFactory | ||
* @param GetCurrentExportedVersion $getCurrentExportedVersion | ||
* @param ExportResource $exportResource | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct( | ||
private readonly CollectionFactory $collectionFactory, | ||
private readonly GetCurrentExportedVersion $getCurrentExportedVersion, | ||
private readonly ExportResource $exportResource, | ||
private readonly LoggerInterface $logger | ||
) { | ||
} | ||
|
||
/** | ||
* Update the status of pending exports | ||
* | ||
* @return void | ||
*/ | ||
public function execute(): void | ||
{ | ||
$currentExportedVersion = $this->getCurrentExportedVersion->execute(); | ||
|
||
/** @var Collection $collection */ | ||
$collection = $this->collectionFactory->create(); | ||
// need to check pending and uploaded exports only | ||
$collection->addFieldToFilter(ExportInterface::FIELD_STATUS, ExportInterface::STATUS_PENDING); | ||
$collection->addFieldToFilter(ExportInterface::FIELD_EXPORT_TYPE, ExportInterface::EXPORT_TYPE_INCREMENTAL); | ||
/** @var Export[] $exports */ | ||
$exports = $collection->getItems(); | ||
foreach ($exports as $export) { | ||
// mark incremental update as invalid if a more recent export has been uploaded already | ||
if ($currentExportedVersion > $export->getVersionId()) { | ||
$export->setStatus(ExportInterface::STATUS_INVALID); | ||
try { | ||
$this->exportResource->save($export); | ||
} catch (\Exception $e) { | ||
$message = sprintf('Error updating status of export %i', $export->getExportId()); | ||
$this->logger->error($message, ['exception' => $e]); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.