Skip to content

Commit

Permalink
opengento#26 add notifiers to actions list
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Oct 14, 2019
1 parent dd91074 commit 864197e
Show file tree
Hide file tree
Showing 32 changed files with 458 additions and 182 deletions.
4 changes: 3 additions & 1 deletion Api/Data/ActionResultInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

namespace Opengento\Gdpr\Api\Data;

use DateTime;

interface ActionResultInterface
{
public function getPerformedAt()/*todo : DateTime*/;
public function getPerformedAt(): DateTime;

public function getState(): string;

Expand Down
4 changes: 2 additions & 2 deletions Api/ExportEntityManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function create(int $entityId, string $entityType, ?string $fileName = nu
* Export all data related to a given entity to the file
*
* @param ExportEntityInterface $exportEntity
* @return string
* @return ExportEntityInterface
* @throws CouldNotSaveException
* @throws LocalizedException
*/
public function export(ExportEntityInterface $exportEntity): string;
public function export(ExportEntityInterface $exportEntity): ExportEntityInterface;
}
8 changes: 6 additions & 2 deletions Console/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand All @@ -20,7 +21,6 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function reset;

class ExportCommand extends Command
{
Expand Down Expand Up @@ -102,8 +102,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
ExportArgumentReader::EXPORT_FILE_NAME => $fileName . '_' . $entityId
]);
$result = $this->action->execute($this->actionContextBuilder->create())->getResult();
/** @var ExportEntityInterface $exportEntity */
$exportEntity = $result[ExportArgumentReader::EXPORT_ENTITY];

$output->writeln('<info>Entity\'s related data have been exported to: ' . reset($result) . '.</info>');
$output->writeln(
'<info>Entity\'s related data have been exported to: ' . $exportEntity->getFilePath() . '.</info>'
);
}
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
Expand Down
9 changes: 6 additions & 3 deletions Controller/Adminhtml/Guest/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
use Opengento\Gdpr\Api\ActionInterface;
use Opengento\Gdpr\Api\Data\ExportEntityInterface;
use Opengento\Gdpr\Controller\Adminhtml\AbstractAction;
use Opengento\Gdpr\Model\Action\ArgumentReader;
use Opengento\Gdpr\Model\Action\ContextBuilder;
use Opengento\Gdpr\Model\Action\Export\ArgumentReader as ExportArgumentReader;
use Opengento\Gdpr\Model\Config;
use function reset;

class Export extends AbstractAction
{
Expand Down Expand Up @@ -63,13 +64,15 @@ protected function executeAction()
]);

try {
$result = $this->action->execute($this->actionContextBuilder->create());
$result = $this->action->execute($this->actionContextBuilder->create())->getResult();
/** @var ExportEntityInterface $exportEntity */
$exportEntity = $result[ExportArgumentReader::EXPORT_ENTITY];

return $this->fileFactory->create(
'guest_privacy_data_' . $entityId . '.zip',
[
'type' => 'filename',
'value' => reset($result),
'value' => $exportEntity->getFilePath(),
'rm' => true,
],
DirectoryList::TMP
Expand Down
8 changes: 6 additions & 2 deletions Controller/Adminhtml/Privacy/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
use Opengento\Gdpr\Api\ActionInterface;
use Opengento\Gdpr\Api\Data\ExportEntityInterface;
use Opengento\Gdpr\Controller\Adminhtml\AbstractAction;
use Opengento\Gdpr\Model\Action\ArgumentReader;
use Opengento\Gdpr\Model\Action\ContextBuilder;
use Opengento\Gdpr\Model\Action\Export\ArgumentReader as ExportArgumentReader;
use Opengento\Gdpr\Model\Config;

class Export extends AbstractAction
Expand Down Expand Up @@ -62,13 +64,15 @@ protected function executeAction()
]);

try {
$result = $this->action->execute($this->actionContextBuilder->create());
$result = $this->action->execute($this->actionContextBuilder->create())->getResult();
/** @var ExportEntityInterface $exportEntity */
$exportEntity = $result[ExportArgumentReader::EXPORT_ENTITY];

return $this->fileFactory->create(
'customer_privacy_data_' . $customerId . '.zip',
[
'type' => 'filename',
'value' => reset($result),
'value' => $exportEntity->getFilePath(),
'rm' => true,
],
DirectoryList::TMP
Expand Down
9 changes: 6 additions & 3 deletions Controller/Adminhtml/Privacy/MassExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
use Magento\Framework\Phrase;
use Magento\Ui\Component\MassAction\Filter;
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\Model\Archive\MoveToArchive;
use function reset;

class MassExport extends AbstractMassAction
{
Expand Down Expand Up @@ -74,8 +75,10 @@ protected function massAction(AbstractCollection $collection)
ArgumentReader::ENTITY_ID => (int) $customerId,
ArgumentReader::ENTITY_TYPE => 'customer'
]);
$result = $this->action->execute($this->actionContextBuilder->create());
$this->moveToArchive->prepareArchive(reset($result), $archiveFileName);
$result = $this->action->execute($this->actionContextBuilder->create())->getResult();
/** @var ExportEntityInterface $exportEntity */
$exportEntity = $result[ExportArgumentReader::EXPORT_ENTITY];
$this->moveToArchive->prepareArchive($exportEntity->getFilePath(), $archiveFileName);
}

return $this->fileFactory->create(
Expand Down
59 changes: 59 additions & 0 deletions Model/Action/Erase/NotifierActionBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Gdpr\Model\Action\Erase;

use InvalidArgumentException;
use Magento\Framework\ObjectManagerInterface;
use Opengento\Gdpr\Api\Data\ActionContextInterface;
use Opengento\Gdpr\Api\Data\ActionResultInterface;
use Opengento\Gdpr\Model\Action\AbstractAction;
use Opengento\Gdpr\Model\Action\ArgumentReader as ActionArgumentReader;
use Opengento\Gdpr\Model\Action\ResultBuilder;
use Opengento\Gdpr\Model\Erase\NotifierInterface;
use function sprintf;

final class NotifierActionBundle extends AbstractAction
{
/**
* @var string[]
*/
private $notifiers;

/**
* @var ObjectManagerInterface
*/
private $objectManager;

public function __construct(
ResultBuilder $resultBuilder,
array $notifiers,
ObjectManagerInterface $objectManager
) {
$this->notifiers = $notifiers;
$this->objectManager = $objectManager;
parent::__construct($resultBuilder);
}

public function execute(ActionContextInterface $actionContext): ActionResultInterface
{
$this->resolveNotifier($actionContext)->notify(ArgumentReader::getEntity($actionContext));

return $this->createActionResult(['is_notify' => true]);
}

private function resolveNotifier(ActionContextInterface $actionContext): NotifierInterface
{
$entityType = ActionArgumentReader::getEntityType($actionContext);

if (!isset($this->notifiers[$entityType])) {
throw new InvalidArgumentException(sprintf('Unknown notifier for entity type "%s".', $entityType));
}

return $this->objectManager->get($this->notifiers[$entityType]);
}
}
3 changes: 2 additions & 1 deletion Model/Action/Export/CreateOrExportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Opengento\Gdpr\Api\Data\ActionResultInterface;
use Opengento\Gdpr\Model\Action\AbstractAction;
use Opengento\Gdpr\Model\Action\ArgumentReader;
use Opengento\Gdpr\Model\Action\Export\ArgumentReader as ExportArgumentReader;
use Opengento\Gdpr\Model\Action\ResultBuilder;
use Opengento\Gdpr\Model\Export\ExportEntityData;

Expand Down Expand Up @@ -40,7 +41,7 @@ public function execute(ActionContextInterface $actionContext): ActionResultInte
}

return $this->createActionResult(
['export_file_path' => $this->exportEntityData->export($entityId, $entityType)]
[ExportArgumentReader::EXPORT_ENTITY => $this->exportEntityData->export($entityId, $entityType)]
);
}
}
3 changes: 2 additions & 1 deletion Model/Action/Export/ExportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Opengento\Gdpr\Api\Data\ActionResultInterface;
use Opengento\Gdpr\Api\ExportEntityManagementInterface;
use Opengento\Gdpr\Model\Action\AbstractAction;
use Opengento\Gdpr\Model\Action\Export\ArgumentReader as ExportArgumentReader;
use Opengento\Gdpr\Model\Action\ResultBuilder;

final class ExportAction extends AbstractAction
Expand All @@ -38,7 +39,7 @@ public function execute(ActionContextInterface $actionContext): ActionResultInte
}

return $this->createActionResult(
['export_file_path' => $this->exportEntityManagement->export($exportEntity)]
[ExportArgumentReader::EXPORT_ENTITY => $this->exportEntityManagement->export($exportEntity)]
);
}
}
59 changes: 59 additions & 0 deletions Model/Action/Export/NotifierActionBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Gdpr\Model\Action\Export;

use InvalidArgumentException;
use Magento\Framework\ObjectManagerInterface;
use Opengento\Gdpr\Api\Data\ActionContextInterface;
use Opengento\Gdpr\Api\Data\ActionResultInterface;
use Opengento\Gdpr\Model\Action\AbstractAction;
use Opengento\Gdpr\Model\Action\ArgumentReader as ActionArgumentReader;
use Opengento\Gdpr\Model\Action\ResultBuilder;
use Opengento\Gdpr\Model\Export\NotifierInterface;
use function sprintf;

final class NotifierActionBundle extends AbstractAction
{
/**
* @var string[]
*/
private $notifiers;

/**
* @var ObjectManagerInterface
*/
private $objectManager;

public function __construct(
ResultBuilder $resultBuilder,
array $notifiers,
ObjectManagerInterface $objectManager
) {
$this->notifiers = $notifiers;
$this->objectManager = $objectManager;
parent::__construct($resultBuilder);
}

public function execute(ActionContextInterface $actionContext): ActionResultInterface
{
$this->resolveNotifier($actionContext)->notify(ArgumentReader::getEntity($actionContext));

return $this->createActionResult(['is_notify' => true]);
}

private function resolveNotifier(ActionContextInterface $actionContext): NotifierInterface
{
$entityType = ActionArgumentReader::getEntityType($actionContext);

if (!isset($this->notifiers[$entityType])) {
throw new InvalidArgumentException(sprintf('Unknown notifier for entity type "%s".', $entityType));
}

return $this->objectManager->get($this->notifiers[$entityType]);
}
}
38 changes: 0 additions & 38 deletions Model/Action/PerformedBy/FrontUser.php

This file was deleted.

44 changes: 44 additions & 0 deletions Model/Action/PerformedBy/NotEmptyStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Gdpr\Model\Action\PerformedBy;

use Opengento\Gdpr\Model\Action\PerformedByInterface;

final class NotEmptyStrategy implements PerformedByInterface
{
private const PERFORMED_BY = 'Unknown';

/**
* @var PerformedByInterface[]
*/
private $performedByList;

/**
* @param PerformedByInterface[] $performedByList
*/
public function __construct(
array $performedByList
) {
$this->performedByList = (static function (PerformedByInterface ...$performedByList) {
return $performedByList;
})(...$performedByList);
}

public function get(): string
{
foreach ($this->performedByList as $performedBy) {
$performer = $performedBy->get();

if (!empty($performer)) {
return $performer;
}
}

return self::PERFORMED_BY;
}
}
Loading

0 comments on commit 864197e

Please sign in to comment.