Skip to content

Commit

Permalink
WIP #30 deferred export implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Sep 17, 2019
1 parent 91e6330 commit 9e9d7e9
Show file tree
Hide file tree
Showing 29 changed files with 872 additions and 151 deletions.
33 changes: 33 additions & 0 deletions Api/ExportEntityCheckerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Gdpr\Api;

/**
* Interface ExportEntityCheckerInterface
* @api
*/
interface ExportEntityCheckerInterface
{
/**
* Check if an export entity already exists for this entity
*
* @param int $entityId
* @param string $entityType
* @return bool
*/
public function exists(int $entityId, string $entityType): bool;

/**
* Check if the document is exported and ready
*
* @param int $entityId
* @param string $entityType
* @return bool
*/
public function isExported(int $entityId, string $entityType): bool;
}
2 changes: 2 additions & 0 deletions Api/ExportEntityManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Opengento\Gdpr\Api;

use Magento\Framework\Exception\AlreadyExistsException;
use Opengento\Gdpr\Api\Data\ExportEntityInterface;

/**
Expand All @@ -22,6 +23,7 @@ interface ExportEntityManagementInterface
* @param string $entityType
* @param null|string $fileName [optional]
* @return ExportEntityInterface
* @throws AlreadyExistsException
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\LocalizedException
*/
Expand Down
1 change: 0 additions & 1 deletion Console/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Magento\Framework\App\State;
use Magento\Framework\Console\Cli;
use Opengento\Gdpr\Api\ExportEntityManagementInterface;
use Opengento\Gdpr\Model\ExportEntity;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down
22 changes: 11 additions & 11 deletions Controller/Adminhtml/Guest/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
use Opengento\Gdpr\Api\ExportEntityManagementInterface;
use Opengento\Gdpr\Controller\Adminhtml\AbstractAction;
use Opengento\Gdpr\Model\Archive\MoveToArchive;
use Opengento\Gdpr\Model\Config;
use Opengento\Gdpr\Model\Export\ExportEntityData;

/**
* Class Export
Expand All @@ -36,27 +36,27 @@ class Export extends AbstractAction
private $moveToArchive;

/**
* @var \Opengento\Gdpr\Api\ExportEntityManagementInterface
* @var ExportEntityData
*/
private $exportManagement;
private $exportEntityData;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Opengento\Gdpr\Model\Config $config
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
* @param \Opengento\Gdpr\Model\Archive\MoveToArchive $moveToArchive
* @param \Opengento\Gdpr\Api\ExportEntityManagementInterface $exportManagement
* @param Context $context
* @param Config $config
* @param FileFactory $fileFactory
* @param MoveToArchive $moveToArchive
* @param ExportEntityData $exportEntityData
*/
public function __construct(
Context $context,
Config $config,
FileFactory $fileFactory,
MoveToArchive $moveToArchive,
ExportEntityManagementInterface $exportManagement
ExportEntityData $exportEntityData
) {
$this->fileFactory = $fileFactory;
$this->moveToArchive = $moveToArchive;
$this->exportManagement = $exportManagement;
$this->exportEntityData = $exportEntityData;
parent::__construct($context, $config);
}

Expand All @@ -67,7 +67,7 @@ protected function executeAction()
{
try {
$entityId = (int) $this->getRequest()->getParam('id');
$fileName = $this->exportManagement->export($this->exportManagement->create($entityId, 'order'));
$fileName = $this->exportEntityData->export($entityId, 'order');
$archiveFileName = 'guest_privacy_data_' . $entityId . '.zip';

return $this->fileFactory->create(
Expand Down
22 changes: 11 additions & 11 deletions Controller/Adminhtml/Privacy/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
use Opengento\Gdpr\Api\ExportEntityManagementInterface;
use Opengento\Gdpr\Controller\Adminhtml\AbstractAction;
use Opengento\Gdpr\Model\Archive\MoveToArchive;
use Opengento\Gdpr\Model\Config;
use Opengento\Gdpr\Model\Export\ExportEntityData;

/**
* Class Export
Expand All @@ -36,27 +36,27 @@ class Export extends AbstractAction
private $moveToArchive;

/**
* @var \Opengento\Gdpr\Api\ExportEntityManagementInterface
* @var ExportEntityData
*/
private $exportManagement;
private $exportEntityData;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Opengento\Gdpr\Model\Config $config
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
* @param \Opengento\Gdpr\Model\Archive\MoveToArchive $moveToArchive
* @param \Opengento\Gdpr\Api\ExportEntityManagementInterface $exportManagement
* @param Context $context
* @param Config $config
* @param FileFactory $fileFactory
* @param MoveToArchive $moveToArchive
* @param ExportEntityData $exportEntityData
*/
public function __construct(
Context $context,
Config $config,
FileFactory $fileFactory,
MoveToArchive $moveToArchive,
ExportEntityManagementInterface $exportManagement
ExportEntityData $exportEntityData
) {
$this->fileFactory = $fileFactory;
$this->moveToArchive = $moveToArchive;
$this->exportManagement = $exportManagement;
$this->exportEntityData = $exportEntityData;
parent::__construct($context, $config);
}

Expand All @@ -67,7 +67,7 @@ protected function executeAction()
{
try {
$customerId = (int) $this->getRequest()->getParam('id');
$fileName = $this->exportManagement->export($this->exportManagement->create($customerId, 'customer'));
$fileName = $this->exportEntityData->export($customerId, 'customer');
$archiveFileName = 'customer_privacy_data_' . $customerId . '.zip';

return $this->fileFactory->create(
Expand Down
24 changes: 12 additions & 12 deletions Controller/Adminhtml/Privacy/MassExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
use Magento\Ui\Component\MassAction\Filter;
use Opengento\Gdpr\Api\ExportEntityManagementInterface;
use Opengento\Gdpr\Model\Archive\MoveToArchive;
use Opengento\Gdpr\Model\Export\ExportEntityData;

/**
* Class MassExport
Expand All @@ -38,29 +38,29 @@ class MassExport extends AbstractMassAction
private $moveToArchive;

/**
* @var \Opengento\Gdpr\Api\ExportEntityManagementInterface
* @var ExportEntityData
*/
private $exportManagement;
private $exportEntityData;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Ui\Component\MassAction\Filter $filter
* @param \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $collectionFactory
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
* @param \Opengento\Gdpr\Model\Archive\MoveToArchive $moveToArchive
* @param \Opengento\Gdpr\Api\ExportEntityManagementInterface $exportManagement
* @param Context $context
* @param Filter $filter
* @param CollectionFactory $collectionFactory
* @param FileFactory $fileFactory
* @param MoveToArchive $moveToArchive
* @param ExportEntityData $exportEntityData
*/
public function __construct(
Context $context,
Filter $filter,
CollectionFactory $collectionFactory,
FileFactory $fileFactory,
MoveToArchive $moveToArchive,
ExportEntityManagementInterface $exportManagement
ExportEntityData $exportEntityData
) {
$this->fileFactory = $fileFactory;
$this->moveToArchive = $moveToArchive;
$this->exportManagement = $exportManagement;
$this->exportEntityData = $exportEntityData;
parent::__construct($context, $filter, $collectionFactory);
}

Expand All @@ -75,7 +75,7 @@ protected function massAction(AbstractCollection $collection)
foreach ($collection->getAllIds() as $customerId) {
$this->moveToArchive->prepareArchive(
$this->moveToArchive->prepareArchive(
$this->exportManagement->export($this->exportManagement->create((int) $customerId, 'customer')),
$this->exportEntityData->export((int) $customerId, 'customer'),
'customer_privacy_data_' . $customerId . '.zip'
),
$archiveFileName
Expand Down
126 changes: 126 additions & 0 deletions Controller/Guest/Download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Gdpr\Controller\Guest;

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Phrase;
use Magento\Framework\Registry;
use Magento\Sales\Controller\AbstractController\OrderLoaderInterface;
use Opengento\Gdpr\Api\ExportEntityRepositoryInterface;
use Opengento\Gdpr\Controller\AbstractGuest;
use Opengento\Gdpr\Model\Archive\MoveToArchive;
use Opengento\Gdpr\Model\Config;

/**
* Class Download Export
*/
class Download extends AbstractGuest
{
/**
* @var \Magento\Framework\App\Response\Http\FileFactory
*/
private $fileFactory;

/**
* @var \Opengento\Gdpr\Model\Archive\MoveToArchive
*/
private $moveToArchive;

/**
* @var ExportEntityRepositoryInterface
*/
private $exportRepository;

/**
* @param Context $context
* @param Config $config
* @param FileFactory $fileFactory
* @param MoveToArchive $moveToArchive
* @param ExportEntityRepositoryInterface $exportRepository
* @param OrderLoaderInterface $orderLoader
* @param Registry $registry
*/
public function __construct(
Context $context,
Config $config,
FileFactory $fileFactory,
MoveToArchive $moveToArchive,
ExportEntityRepositoryInterface $exportRepository,
OrderLoaderInterface $orderLoader,
Registry $registry
) {
$this->fileFactory = $fileFactory;
$this->moveToArchive = $moveToArchive;
$this->exportRepository = $exportRepository;
parent::__construct($context, $config, $orderLoader, $registry);
}

/**
* @inheritdoc
*/
protected function isAllowed(): bool
{
return parent::isAllowed() && $this->config->isExportEnabled();
}

/**
* @inheritdoc
*/
protected function executeAction()
{
try {
$this->download();
} catch (NoSuchEntityException $e) {
$this->messageManager->addErrorMessage(
new Phrase('The document does not exists and may have expired. Please renew your demand.')
);
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage($e, new Phrase('Something went wrong, please try again later!'));
}

/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

return $resultRedirect->setRefererOrBaseUrl();
}

/**
* Download the export document
*
* @return ResponseInterface
* @throws NoSuchEntityException
* @throws \Magento\Framework\Exception\FileSystemException
* @throws \Magento\Framework\Exception\NotFoundException
* @throws \Exception
*/
private function download(): ResponseInterface
{
/** @var \Magento\Sales\Api\Data\OrderInterface $order */
$order = $this->registry->registry('current_order');
$export = $this->exportRepository->getByEntity($this->retrieveOrderId(), 'order');
$archiveFileName = 'customer_privacy_data_' . $order->getCustomerLastname() . '.zip';

return $this->fileFactory->create(
$archiveFileName,
[
'type' => 'filename',
'value' => $this->moveToArchive->prepareArchive($export->getFilePath(), $archiveFileName),
'rm' => true,
],
DirectoryList::TMP
);
}
}
Loading

0 comments on commit 9e9d7e9

Please sign in to comment.