Skip to content

Commit

Permalink
#138 - WIP Remove ActionInterface abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Jun 17, 2024
1 parent db16ac2 commit 8685359
Show file tree
Hide file tree
Showing 189 changed files with 354 additions and 2,205 deletions.
1 change: 1 addition & 0 deletions Api/ActionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

/**
* @api
* @todo remove
*/
interface ActionInterface
{
Expand Down
36 changes: 0 additions & 36 deletions Block/Adminhtml/Action/Edit/BackButton.php

This file was deleted.

29 changes: 0 additions & 29 deletions Block/Adminhtml/Action/Edit/ExecuteButton.php

This file was deleted.

24 changes: 0 additions & 24 deletions Block/Adminhtml/Action/Edit/ResetButton.php

This file was deleted.

2 changes: 1 addition & 1 deletion Block/Adminhtml/Customer/Edit/EraseButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Opengento\Gdpr\Api\EraseEntityCheckerInterface;
use Opengento\Gdpr\Model\Config;

final class EraseButton extends GenericButton implements ButtonProviderInterface
class EraseButton extends GenericButton implements ButtonProviderInterface
{
private EraseEntityCheckerInterface $eraseCustomerChecker;

Expand Down
2 changes: 1 addition & 1 deletion Block/Adminhtml/Customer/Edit/ExportButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
use Opengento\Gdpr\Model\Config;

final class ExportButton extends GenericButton implements ButtonProviderInterface
class ExportButton extends GenericButton implements ButtonProviderInterface
{
private Config $config;

Expand Down
41 changes: 15 additions & 26 deletions Console/Command/EraseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

namespace Opengento\Gdpr\Console\Command;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Console\Cli;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Opengento\Gdpr\Api\ActionInterface;
use Opengento\Gdpr\Model\Action\ArgumentReader;
use Opengento\Gdpr\Model\Action\ContextBuilder;
use Opengento\Gdpr\Api\Data\EraseEntityInterface;
use Opengento\Gdpr\Api\EraseEntityManagementInterface;
use Opengento\Gdpr\Api\EraseEntityRepositoryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -25,25 +26,14 @@ class EraseCommand extends Command
private const INPUT_ARGUMENT_ENTITY_ID = 'entity_id';
private const INPUT_ARGUMENT_ENTITY_TYPE = 'entity_type';

private State $appState;

private Registry $registry;

private ActionInterface $action;

private ContextBuilder $actionContextBuilder;

public function __construct(
State $appState,
Registry $registry,
ActionInterface $action,
ContextBuilder $actionContextBuilder,
private State $appState,
private Registry $registry,
private EraseEntityManagementInterface $eraseManagement,
private EraseEntityRepositoryInterface $eraseEntityRepository,
private SearchCriteriaBuilder $searchCriteriaBuilder,
string $name = 'gdpr:entity:erase'
) {
$this->appState = $appState;
$this->registry = $registry;
$this->action = $action;
$this->actionContextBuilder = $actionContextBuilder;
parent::__construct($name);
}

Expand Down Expand Up @@ -82,15 +72,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$entityType = $input->getArgument(self::INPUT_ARGUMENT_ENTITY_TYPE);

try {
foreach ($entityIds as $entityId) {
$this->action->execute(
$this->actionContextBuilder->setParameters(
[ArgumentReader::ENTITY_ID => $entityId, ArgumentReader::ENTITY_TYPE => $entityType]
)->create()
);
$this->searchCriteriaBuilder->addFilter(EraseEntityInterface::ENTITY_ID, $entityIds, 'in');
$this->searchCriteriaBuilder->addFilter(EraseEntityInterface::ENTITY_TYPE, $entityType);
$eraseEntityList = $this->eraseEntityRepository->getList($this->searchCriteriaBuilder->create());
foreach ($eraseEntityList->getItems() as $eraseEntity) {
$this->eraseManagement->process($eraseEntity);

$output->writeln(
'<info>Entity\'s (' . $entityType . ') with ID "' . $entityId . '" has been erased.</info>'
'<info>Entity\'s (' . $entityType . ') with ID "' . $eraseEntity->getEntityId() . '" has been erased.</info>'
);
}
} catch (LocalizedException $e) {
Expand Down
37 changes: 12 additions & 25 deletions Console/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
namespace Opengento\Gdpr\Console\Command;

use Exception;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Console\Cli;
use Magento\Framework\Exception\LocalizedException;
use Opengento\Gdpr\Api\ActionInterface;
use Opengento\Gdpr\Api\Data\ExportEntityInterface;
use Opengento\Gdpr\Model\Action\ArgumentReader;
use Opengento\Gdpr\Model\Action\ContextBuilder;
use Opengento\Gdpr\Model\Action\Export\ArgumentReader as ExportArgumentReader;
use Opengento\Gdpr\Api\ExportEntityManagementInterface;
use Opengento\Gdpr\Api\ExportEntityRepositoryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -29,21 +28,13 @@ class ExportCommand extends Command
private const INPUT_ARGUMENT_ENTITY_TYPE = 'entity_type';
private const INPUT_OPTION_FILENAME = 'filename';

private State $appState;

private ActionInterface $action;

private ContextBuilder $actionContextBuilder;

public function __construct(
State $appState,
ActionInterface $action,
ContextBuilder $actionContextBuilder,
private State $appState,
private ExportEntityManagementInterface $exportEntityManagement,
private ExportEntityRepositoryInterface $exportEntityRepository,
private SearchCriteriaBuilder $searchCriteriaBuilder,
string $name = 'gdpr:entity:export'
) {
$this->appState = $appState;
$this->action = $action;
$this->actionContextBuilder = $actionContextBuilder;
parent::__construct($name);
}

Expand Down Expand Up @@ -87,15 +78,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$fileName = $input->getOption(self::INPUT_OPTION_FILENAME);

try {
foreach ($entityIds as $entityId) {
$this->actionContextBuilder->setParameters([
ArgumentReader::ENTITY_ID => $entityId,
ArgumentReader::ENTITY_TYPE => $entityType,
ExportArgumentReader::EXPORT_FILE_NAME => $fileName . '_' . $entityId
]);
$result = $this->action->execute($this->actionContextBuilder->create())->getResult();
/** @var ExportEntityInterface $exportEntity */
$exportEntity = $result[ExportArgumentReader::EXPORT_ENTITY];
$this->searchCriteriaBuilder->addFilter(ExportEntityInterface::ENTITY_ID, $entityIds, 'in');
$this->searchCriteriaBuilder->addFilter(ExportEntityInterface::ENTITY_TYPE, $entityType);
$exportEntityList = $this->exportEntityRepository->getList($this->searchCriteriaBuilder->create());
foreach ($exportEntityList->getItems() as $exportEntity) {
$this->exportEntityManagement->export($exportEntity);

$output->writeln(
'<info>Entity\'s related data have been exported to: ' . $exportEntity->getFilePath() . '.</info>'
Expand Down
30 changes: 8 additions & 22 deletions Controller/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,31 @@

abstract class AbstractAction implements ActionInterface
{
protected RequestInterface $request;

protected ResultFactory $resultFactory;

protected ManagerInterface $messageManager;

protected Config $config;

public function __construct(
RequestInterface $request,
ResultFactory $resultFactory,
ManagerInterface $messageManager,
Config $config
) {
$this->request = $request;
$this->resultFactory = $resultFactory;
$this->messageManager = $messageManager;
$this->config = $config;
}
protected RequestInterface $request,
protected ResultFactory $resultFactory,
protected ManagerInterface $messageManager,
protected Config $config
) {}

public function execute()
public function execute(): ResultInterface|ResponseInterface
{
return $this->isAllowed() ? $this->executeAction() : $this->forwardNoRoute();
}

/**
* Execute action based on request and return result
*
* @return ResultInterface|ResponseInterface
* @throws NotFoundException
*/
abstract protected function executeAction();
abstract protected function executeAction(): ResultInterface|ResponseInterface;

protected function isAllowed(): bool
{
return $this->config->isModuleEnabled();
}

protected function forwardNoRoute(): ResultInterface
final protected function forwardNoRoute(): ResultInterface
{
/** @var Forward $resultForward */
$resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
Expand Down
18 changes: 5 additions & 13 deletions Controller/AbstractGuest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Opengento\Gdpr\Controller;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Message\ManagerInterface;
Expand All @@ -18,34 +19,25 @@

abstract class AbstractGuest extends AbstractAction
{
/**
* @var OrderLoaderInterface
*/
protected OrderLoaderInterface $orderLoader;

protected Registry $registry;

public function __construct(
RequestInterface $request,
ResultFactory $resultFactory,
ManagerInterface $messageManager,
Config $config,
OrderLoaderInterface $orderLoader,
Registry $registry
protected OrderLoaderInterface $orderLoader,
protected Registry $registry
) {
$this->orderLoader = $orderLoader;
$this->registry = $registry;
parent::__construct($request, $resultFactory, $messageManager, $config);
}

public function execute()
final public function execute(): ResultInterface|ResponseInterface
{
$result = $this->orderLoader->load($this->request);

return $result instanceof ResultInterface ? $result : parent::execute();
}

protected function currentOrder(): OrderInterface
final protected function currentOrder(): OrderInterface
{
return $this->registry->registry('current_order');
}
Expand Down
Loading

0 comments on commit 8685359

Please sign in to comment.