Skip to content

Commit

Permalink
Fix multiple errors + remove unused types + fix admin actions
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Aug 24, 2024
1 parent 285e688 commit 3597dc2
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 101 deletions.
3 changes: 2 additions & 1 deletion Block/Adminhtml/Config/Form/Field/AttributesAnonymizers.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getAnonymizersSelectRenderer(): Select
);
}

return $this->getData('anonymizers_select_renderer');
return $this->_getData('anonymizers_select_renderer');
}

/**
Expand All @@ -53,6 +53,7 @@ protected function _prepareToRender(): void
'anonymizer',
[
'label' => new Phrase('Anonymizer'),
'class' => 'required-entry',
'renderer' => $this->getAnonymizersSelectRenderer(),
]
);
Expand Down
41 changes: 26 additions & 15 deletions Controller/Adminhtml/Guest/Erase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,42 @@
namespace Opengento\Gdpr\Controller\Adminhtml\Guest;

use Exception;
use Magento\Backend\Model\View\Result\RedirectFactory;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Phrase;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Opengento\Gdpr\Api\Data\EraseEntityInterface;
use Opengento\Gdpr\Api\EraseEntityManagementInterface;
use Opengento\Gdpr\Api\EraseEntityRepositoryInterface;
use Opengento\Gdpr\Model\Config;

class Erase implements HttpPostActionInterface
class Erase extends Action
{
public const ADMIN_RESOURCE = 'Opengento_Gdpr::order_erase';

public function __construct(
private RequestInterface $request,
private ManagerInterface $messageManager,
Context $context,
private StoreManagerInterface $storeManager,
private OrderRepositoryInterface $orderRepository,
private EraseEntityManagementInterface $eraseEntityManagement,
private EraseEntityRepositoryInterface $eraseEntityRepository,
private Config $config,
private RedirectFactory $redirectFactory,
) {}
private Config $config
) {
parent::__construct($context);
}

public function execute(): ResultInterface|ResponseInterface
{
try {
$orderId = (int)$this->request->getParam('id');
$orderId = (int)$this->getRequest()->getParam('id');
if ($this->isOrderErasureEnabled($orderId)) {
$this->eraseEntityManagement->process(
$this->eraseEntityRepository->getByEntity($orderId, 'order')
);
$this->eraseEntityManagement->process($this->fetchEntity($orderId));
$this->messageManager->addSuccessMessage(new Phrase('You erased the order.'));
}
} catch (LocalizedException $e) {
Expand All @@ -54,7 +52,7 @@ public function execute(): ResultInterface|ResponseInterface
$this->messageManager->addExceptionMessage($e, new Phrase('An error occurred on the server.'));
}

return $this->redirectFactory->create()->setPath('sales/order/index');
return $this->resultRedirectFactory->create()->setPath('sales/order/index');
}

/**
Expand All @@ -66,4 +64,17 @@ private function isOrderErasureEnabled(int $orderId): bool
$this->storeManager->getStore($this->orderRepository->get($orderId)->getStoreId())->getWebsiteId()
);
}

/**
* @throws CouldNotSaveException
* @throws LocalizedException
*/
private function fetchEntity(int $orderId): EraseEntityInterface
{
try {
return $this->eraseEntityRepository->getByEntity($orderId, 'order');
} catch (NoSuchEntityException) {
return $this->eraseEntityManagement->create($orderId, 'order');
}
}
}
41 changes: 27 additions & 14 deletions Controller/Adminhtml/Guest/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,46 @@
namespace Opengento\Gdpr\Controller\Adminhtml\Guest;

use Exception;
use Magento\Backend\Model\View\Result\RedirectFactory;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Phrase;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Opengento\Gdpr\Api\Data\ExportEntityInterface;
use Opengento\Gdpr\Api\ExportEntityManagementInterface;
use Opengento\Gdpr\Api\ExportEntityRepositoryInterface;
use Opengento\Gdpr\Model\Config;

class Export implements HttpGetActionInterface
class Export extends Action
{
public const ADMIN_RESOURCE = 'Opengento_Gdpr::order_export';

public function __construct(
private RequestInterface $request,
private ManagerInterface $messageManager,
Context $context,
private StoreManagerInterface $storeManager,
private OrderRepositoryInterface $orderRepository,
private ExportEntityManagementInterface $exportEntityManagement,
private ExportEntityRepositoryInterface $exportEntityRepository,
private Config $config,
private RedirectFactory $redirectFactory,
private FileFactory $fileFactory
) {}
) {
parent::__construct($context);
}

public function execute(): ResultInterface|ResponseInterface
{
try {
$orderId = (int)$this->request->getParam('id');
$orderId = (int)$this->getRequest()->getParam('id');
if ($this->isOrderExportEnabled($orderId)) {
$exportEntity = $this->exportEntityManagement->export(
$this->exportEntityRepository->getByEntity($orderId, 'order')
);
$exportEntity = $this->exportEntityManagement->export($this->fetchEntity($orderId));

return $this->fileFactory->create(
'guest_privacy_data_' . $exportEntity->getEntityId() . '.zip',
Expand All @@ -65,7 +64,7 @@ public function execute(): ResultInterface|ResponseInterface
$this->messageManager->addExceptionMessage($e, new Phrase('An error occurred on the server.'));
}

return $this->redirectFactory->create()->setRefererOrBaseUrl();
return $this->resultRedirectFactory->create()->setRefererOrBaseUrl();
}

/**
Expand All @@ -77,4 +76,18 @@ private function isOrderExportEnabled(int $orderId): bool
$this->storeManager->getStore($this->orderRepository->get($orderId)->getStoreId())->getWebsiteId()
);
}

/**
* @throws AlreadyExistsException
* @throws CouldNotSaveException
* @throws LocalizedException
*/
private function fetchEntity(int $orderId): ExportEntityInterface
{
try {
return $this->exportEntityRepository->getByEntity($orderId, 'order');
} catch (NoSuchEntityException) {
return $this->exportEntityManagement->create($orderId, 'order');
}
}
}
40 changes: 26 additions & 14 deletions Controller/Adminhtml/Privacy/Erase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,40 @@
namespace Opengento\Gdpr\Controller\Adminhtml\Privacy;

use Exception;
use Magento\Backend\Model\View\Result\RedirectFactory;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Phrase;
use Opengento\Gdpr\Api\Data\EraseEntityInterface;
use Opengento\Gdpr\Api\EraseEntityManagementInterface;
use Opengento\Gdpr\Api\EraseEntityRepositoryInterface;
use Opengento\Gdpr\Model\Config;

class Erase implements HttpPostActionInterface
class Erase extends Action
{
public const ADMIN_RESOURCE = 'Opengento_Gdpr::customer_erase';

public function __construct(
private RequestInterface $request,
private ManagerInterface $messageManager,
Context $context,
private CustomerRepositoryInterface $customerRepository,
private EraseEntityManagementInterface $eraseEntityManagement,
private EraseEntityRepositoryInterface $eraseEntityRepository,
private RedirectFactory $redirectFactory,
private Config $config
) {}
) {
parent::__construct($context);
}

public function execute(): ResultInterface|ResponseInterface
{
try {
$customerId = (int)$this->request->getParam('id');
$customerId = (int)$this->getRequest()->getParam('id');
if ($this->config->isErasureEnabled($this->customerRepository->getById($customerId)->getWebsiteId())) {
$this->eraseEntityManagement->process(
$this->eraseEntityRepository->getByEntity($customerId, 'customer')
);
$this->eraseEntityManagement->process($this->fetchEntity($customerId));
$this->messageManager->addSuccessMessage(new Phrase('You erased the customer.'));
}
} catch (LocalizedException $e) {
Expand All @@ -51,6 +50,19 @@ public function execute(): ResultInterface|ResponseInterface
$this->messageManager->addExceptionMessage($e, new Phrase('An error occurred on the server.'));
}

return $this->redirectFactory->create()->setPath('customer/index');
return $this->resultRedirectFactory->create()->setPath('customer/index');
}

/**
* @throws CouldNotSaveException
* @throws LocalizedException
*/
private function fetchEntity(int $customerId): EraseEntityInterface
{
try {
return $this->eraseEntityRepository->getByEntity($customerId, 'customer');
} catch (NoSuchEntityException) {
return $this->eraseEntityManagement->create($customerId, 'customer');
}
}
}
42 changes: 28 additions & 14 deletions Controller/Adminhtml/Privacy/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,44 @@
namespace Opengento\Gdpr\Controller\Adminhtml\Privacy;

use Exception;
use Magento\Backend\Model\View\Result\RedirectFactory;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Phrase;
use Opengento\Gdpr\Api\Data\ExportEntityInterface;
use Opengento\Gdpr\Api\ExportEntityManagementInterface;
use Opengento\Gdpr\Api\ExportEntityRepositoryInterface;
use Opengento\Gdpr\Model\Config;

class Export implements HttpGetActionInterface
class Export extends Action
{
public const ADMIN_RESOURCE = 'Opengento_Gdpr::customer_export';

public function __construct(
private RequestInterface $request,
private ManagerInterface $messageManager,
Context $context,
private CustomerRepositoryInterface $customerRepository,
private ExportEntityManagementInterface $exportEntityManagement,
private ExportEntityRepositoryInterface $exportEntityRepository,
private RedirectFactory $redirectFactory,
private Config $config,
private FileFactory $fileFactory
) {}
) {
parent::__construct($context);
}

public function execute(): ResultInterface|ResponseInterface
{
try {
$customerId = (int)$this->request->getParam('id');
$customerId = (int)$this->getRequest()->getParam('id');
if ($this->config->isExportEnabled($this->customerRepository->getById($customerId)->getWebsiteId())) {
$exportEntity = $this->exportEntityManagement->export(
$this->exportEntityRepository->getByEntity($customerId, 'customer')
);
$exportEntity = $this->exportEntityManagement->export($this->fetchEntity($customerId));

return $this->fileFactory->create(
'customer_privacy_data_' . $exportEntity->getEntityId() . '.zip',
Expand All @@ -62,6 +62,20 @@ public function execute(): ResultInterface|ResponseInterface
$this->messageManager->addExceptionMessage($e, new Phrase('An error occurred on the server.'));
}

return $this->redirectFactory->create()->setRefererOrBaseUrl();
return $this->resultRedirectFactory->create()->setRefererOrBaseUrl();
}

/**
* @throws AlreadyExistsException
* @throws CouldNotSaveException
* @throws LocalizedException
*/
private function fetchEntity(int $customerId): ExportEntityInterface
{
try {
return $this->exportEntityRepository->getByEntity($customerId, 'customer');
} catch (NoSuchEntityException) {
return $this->exportEntityManagement->create($customerId, 'customer');
}
}
}
37 changes: 0 additions & 37 deletions Model/Config/Source/VirtualEntityAttributes.php

This file was deleted.

3 changes: 2 additions & 1 deletion Model/Erase/NotifyEraseEntityManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ public function create(int $entityId, string $entityType): EraseEntityInterface

public function cancel(int $entityId, string $entityType): bool
{
$eraseEntity = $this->eraseRepository->getByEntity($entityId, $entityType);
$canceled = $this->eraseManagement->cancel($entityId, $entityType);
if ($canceled) {
$this->notifierRepository->get($entityType, 'cancel')->notify($this->eraseRepository->getByEntity($entityId, $entityType));
$this->notifierRepository->get($entityType, 'cancel')->notify($eraseEntity);
}

return $canceled;
Expand Down
Loading

0 comments on commit 3597dc2

Please sign in to comment.