diff --git a/Api/Data/ExportEntityInterface.php b/Api/Data/ExportEntityInterface.php index 560a753..1c36abf 100644 --- a/Api/Data/ExportEntityInterface.php +++ b/Api/Data/ExportEntityInterface.php @@ -49,7 +49,7 @@ public function setFileName(string $filename): ExportEntityInterface; public function getFilePath(): ?string; - public function setFilePath(string $filePath): ExportEntityInterface; + public function setFilePath(?string $filePath): ExportEntityInterface; public function getCreatedAt(): string; diff --git a/Console/Command/EraseCommand.php b/Console/Command/EraseCommand.php index 6f4d7fb..ff39efc 100644 --- a/Console/Command/EraseCommand.php +++ b/Console/Command/EraseCommand.php @@ -20,9 +20,6 @@ class EraseCommand extends Command { - /** - * Input Variables Names - */ private const INPUT_ARGUMENT_ENTITY_ID = 'entity_id'; private const INPUT_ARGUMENT_ENTITY_TYPE = 'entity_type'; diff --git a/Console/Command/ExportCommand.php b/Console/Command/ExportCommand.php index 06e0eb6..3e8a9af 100644 --- a/Console/Command/ExportCommand.php +++ b/Console/Command/ExportCommand.php @@ -20,9 +20,6 @@ class ExportCommand extends Command { - /** - * Input Variables Names - */ private const INPUT_ARGUMENT_ENTITY_ID = 'entity_id'; private const INPUT_ARGUMENT_ENTITY_TYPE = 'entity_type'; private const INPUT_OPTION_FILENAME = 'filename'; diff --git a/Model/Archive/MoveToArchive.php b/Model/Archive/MoveToArchive.php index 27c170d..287e308 100644 --- a/Model/Archive/MoveToArchive.php +++ b/Model/Archive/MoveToArchive.php @@ -50,6 +50,9 @@ public function prepareArchive(string $source, string $destination): string throw new NotFoundException(new Phrase('File "%1" does not exists.', [$source])); } - return $this->archive->pack($source, $tmpWrite->getAbsolutePath($destination)); + $archive = $this->archive->pack($source, $tmpWrite->getAbsolutePath($destination)); + $fileDriver->deleteFile($source); + + return $archive; } } diff --git a/Model/Archive/Zip.php b/Model/Archive/Zip.php index 9e3ba2d..c61a31a 100644 --- a/Model/Archive/Zip.php +++ b/Model/Archive/Zip.php @@ -10,6 +10,7 @@ use Magento\Framework\Archive\ArchiveInterface; use Magento\Framework\Archive\Zip as ArchiveZip; use Magento\Framework\Filesystem; +use function basename; /** * Zip compressed file archive with local file name. @@ -41,7 +42,7 @@ public function pack($source, $destination): string $zip = new \ZipArchive(); $zip->open($destination, \ZipArchive::CREATE); - $zip->addFile($source, $directoryRead->isDirectory($source) ? '' : \basename($source)); + $zip->addFile($source, $directoryRead->isDirectory($source) ? '' : basename($source)); $zip->close(); return $destination; diff --git a/Model/Config/Source/EraseComponents.php b/Model/Config/Source/EraseComponents.php index 4e87046..905f0b5 100644 --- a/Model/Config/Source/EraseComponents.php +++ b/Model/Config/Source/EraseComponents.php @@ -10,6 +10,8 @@ use Magento\Framework\Data\OptionSourceInterface; use Magento\Framework\ObjectManager\ConfigInterface; use Magento\Framework\Phrase; +use function array_keys; +use function array_merge; final class EraseComponents implements OptionSourceInterface { @@ -66,7 +68,7 @@ private function retrieveDelegateProcessors(): array } } - return \array_keys(\array_merge(...$delegateProcessors)); + return array_keys(array_merge(...$delegateProcessors)); } /** diff --git a/Model/Config/Source/VirtualArrayArgumentList.php b/Model/Config/Source/VirtualArrayArgumentList.php index 51fdc08..8dfb9b3 100644 --- a/Model/Config/Source/VirtualArrayArgumentList.php +++ b/Model/Config/Source/VirtualArrayArgumentList.php @@ -10,6 +10,7 @@ use Magento\Framework\Data\OptionSourceInterface; use Magento\Framework\ObjectManager\ConfigInterface; use Magento\Framework\Phrase; +use function array_keys; final class VirtualArrayArgumentList implements OptionSourceInterface { @@ -46,7 +47,7 @@ public function __construct( public function toOptionArray(): array { if (!$this->options) { - foreach (\array_keys($this->retrieveItems()) as $item) { + foreach (array_keys($this->retrieveItems()) as $item) { $this->options[] = ['value' => $item, 'label' => new Phrase($item)]; } } diff --git a/Model/Config/Source/VirtualCustomerAttributes.php b/Model/Config/Source/VirtualCustomerAttributes.php index cb2ca1b..a436688 100644 --- a/Model/Config/Source/VirtualCustomerAttributes.php +++ b/Model/Config/Source/VirtualCustomerAttributes.php @@ -24,38 +24,29 @@ final class VirtualCustomerAttributes implements OptionSourceInterface private $options; public function __construct( - MetadataInterface $metadata, - array $options = [] + MetadataInterface $metadata ) { $this->metadata = $metadata; - $this->options = $this->loadOptions($options); + $this->options = []; } public function toOptionArray(): array { - return $this->options; - } - - /** - * Load an prepare customer address attributes options - * - * @param array $defaultOptions [optional] - * @return array - */ - public function loadOptions(array $defaultOptions = []): array - { - $options = []; - - try { - $attributes = $this->metadata->getAllAttributesMetadata(); - } catch (LocalizedException $e) { - $attributes = []; + if (!$this->options) { + try { + $attributes = $this->metadata->getAllAttributesMetadata(); + } catch (LocalizedException $e) { + $attributes = []; + } + + foreach ($attributes as $attribute) { + $this->options[] = [ + 'value' => $attribute->getAttributeCode(), + 'label' => $attribute->getFrontendLabel(), + ]; + } } - foreach ($attributes as $attribute) { - $options[] = ['value' => $attribute->getAttributeCode(), 'label' => $attribute->getFrontendLabel()]; - } - - return \array_merge($defaultOptions, $options); + return $this->options; } } diff --git a/Model/Config/Source/VirtualEntityAttributes.php b/Model/Config/Source/VirtualEntityAttributes.php index 381fc6c..a8ce575 100644 --- a/Model/Config/Source/VirtualEntityAttributes.php +++ b/Model/Config/Source/VirtualEntityAttributes.php @@ -9,6 +9,7 @@ use Magento\Framework\Data\OptionSourceInterface; use Magento\Framework\Model\EntitySnapshot\AttributeProviderInterface; +use function array_keys; /** * Class VirtualEntityAttributes @@ -44,7 +45,7 @@ public function __construct( public function toOptionArray(): array { if (!$this->options) { - foreach (\array_keys($this->attributeProvider->getAttributes($this->entityType)) as $attribute) { + foreach (array_keys($this->attributeProvider->getAttributes($this->entityType)) as $attribute) { $this->options[] = ['value' => $attribute, 'label' => $attribute]; } } diff --git a/Model/Entity/DataCollector.php b/Model/Entity/DataCollector.php index c3eaab7..5255e6b 100644 --- a/Model/Entity/DataCollector.php +++ b/Model/Entity/DataCollector.php @@ -27,12 +27,12 @@ public function __construct( $this->document = $document; } - public function collect($entity): array + public function collect(object $entity): array { $this->entityIterator->iterate($entity); $data = $this->document->getData(); $this->document->setData([]); - return $data; + return $data; } } diff --git a/Model/Entity/DataCollectorGeneric.php b/Model/Entity/DataCollectorGeneric.php index c958542..dec7261 100644 --- a/Model/Entity/DataCollectorGeneric.php +++ b/Model/Entity/DataCollectorGeneric.php @@ -42,7 +42,7 @@ public function __construct( * @inheritdoc * @throws Exception */ - public function collect($entity): array + public function collect(object $entity): array { $entityType = $this->typeResolver->resolve($entity); diff --git a/Model/Export/ExportToFile.php b/Model/Export/ExportToFile.php new file mode 100644 index 0000000..61f522f --- /dev/null +++ b/Model/Export/ExportToFile.php @@ -0,0 +1,83 @@ +exportProcessorFactory = $exportProcessorFactory; + $this->exportRendererFactory = $exportRendererFactory; + $this->archive = $archive; + $this->config = $config; + } + + /** + * @param ExportEntityInterface $exportEntity + * @return string|null + * @throws FileSystemException + * @throws NotFoundException + */ + public function export(ExportEntityInterface $exportEntity): ?string + { + $exporter = $this->exportProcessorFactory->get($exportEntity->getEntityType()); + $fileName = $this->prepareFileName($exportEntity); + $data = $exporter->execute($exportEntity->getEntityId(), []); + foreach ($this->config->getExportRendererCodes() as $rendererCode) { + $filePath = $this->archive->prepareArchive( + $this->exportRendererFactory->get($rendererCode)->saveData($fileName, $data), + $fileName . '.zip' + ); + } + + return $filePath ?? null; + } + + private function prepareFileName(ExportEntityInterface $exportEntity): string + { + return 'gdpr' . + DIRECTORY_SEPARATOR . + sha1($exportEntity->getEntityType() . $exportEntity->getExportId()) . + DIRECTORY_SEPARATOR . + $exportEntity->getFileName(); + } +} diff --git a/Model/ExportEntity.php b/Model/ExportEntity.php index 2a6a9bb..4af187f 100644 --- a/Model/ExportEntity.php +++ b/Model/ExportEntity.php @@ -65,7 +65,7 @@ public function getFilePath(): ?string return $this->_getData(self::FILE_PATH) === null ? null : (string) $this->_getData(self::FILE_PATH); } - public function setFilePath(string $filePath): ExportEntityInterface + public function setFilePath(?string $filePath): ExportEntityInterface { return $this->setData(self::FILE_PATH, $filePath); } diff --git a/Model/ExportEntityManagement.php b/Model/ExportEntityManagement.php index 569390e..3960f5b 100644 --- a/Model/ExportEntityManagement.php +++ b/Model/ExportEntityManagement.php @@ -9,9 +9,6 @@ use Exception; use Magento\Framework\Exception\AlreadyExistsException; -use Magento\Framework\Exception\FileSystemException; -use Magento\Framework\Exception\LocalizedException; -use Magento\Framework\Exception\NotFoundException; use Magento\Framework\Phrase; use Magento\Framework\Stdlib\DateTime; use Opengento\Gdpr\Api\Data\ExportEntityInterface; @@ -19,11 +16,7 @@ use Opengento\Gdpr\Api\ExportEntityCheckerInterface; use Opengento\Gdpr\Api\ExportEntityManagementInterface; use Opengento\Gdpr\Api\ExportEntityRepositoryInterface; -use Opengento\Gdpr\Model\Archive\MoveToArchive; -use Opengento\Gdpr\Service\Export\ProcessorFactory; -use Opengento\Gdpr\Service\Export\RendererFactory; -use function sha1; -use const DIRECTORY_SEPARATOR; +use Opengento\Gdpr\Model\Export\ExportToFile; final class ExportEntityManagement implements ExportEntityManagementInterface { @@ -43,19 +36,9 @@ final class ExportEntityManagement implements ExportEntityManagementInterface private $exportEntityChecker; /** - * @var ProcessorFactory + * @var ExportToFile */ - private $exportProcessorFactory; - - /** - * @var RendererFactory - */ - private $exportRendererFactory; - - /** - * @var MoveToArchive - */ - private $archive; + private $exportToFile; /** * @var Config @@ -66,17 +49,13 @@ public function __construct( ExportEntityInterfaceFactory $exportEntityFactory, ExportEntityRepositoryInterface $exportEntityRepository, ExportEntityCheckerInterface $exportEntityChecker, - ProcessorFactory $exportProcessorFactory, - RendererFactory $exportRendererFactory, - MoveToArchive $archive, + ExportToFile $exportToFile, Config $config ) { $this->exportEntityFactory = $exportEntityFactory; $this->exportEntityRepository = $exportEntityRepository; $this->exportEntityChecker = $exportEntityChecker; - $this->exportProcessorFactory = $exportProcessorFactory; - $this->exportRendererFactory = $exportRendererFactory; - $this->archive = $archive; + $this->exportToFile = $exportToFile; $this->config = $config; } @@ -103,50 +82,17 @@ public function create(int $entityId, string $entityType, ?string $fileName = nu /** * @inheritdoc - * @throws FileSystemException - * @throws NotFoundException * @throws Exception */ public function export(ExportEntityInterface $exportEntity): string { - $exporter = $this->exportProcessorFactory->get($exportEntity->getEntityType()); - $fileName = $this->prepareFileName($exportEntity); - $data = $exporter->execute($exportEntity->getEntityId(), []); - foreach ($this->config->getExportRendererCodes() as $rendererCode) { - $filePath = $this->archive->prepareArchive( - $this->exportRendererFactory->get($rendererCode)->saveData($fileName, $data), - $fileName . '.zip' - ); - } - //todo remove files after bundling - - if (!isset($filePath)) { - throw new LocalizedException( - new Phrase( - 'The archive cannot be created for the entity type %& with ID %2.', - [$exportEntity->getEntityType(), $exportEntity->getEntityId()] - ) - ); - } - - $exportEntity->setFilePath($filePath); + $exportEntity->setFilePath($this->exportToFile->export($exportEntity)); $exportEntity->setExpiredAt( (new \DateTime('+' . $this->config->getExportLifetime() . 'minutes'))->format(DateTime::DATETIME_PHP_FORMAT) ); - $exportEntity->setExportedAt( - (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT) - ); + $exportEntity->setExportedAt((new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT)); $this->exportEntityRepository->save($exportEntity); - return $filePath; - } - - private function prepareFileName(ExportEntityInterface $exportEntity): string - { - return 'gdpr' . - DIRECTORY_SEPARATOR . - sha1($exportEntity->getEntityType() . $exportEntity->getExportId()) . - DIRECTORY_SEPARATOR . - $exportEntity->getFileName(); + return $exportEntity->getFilePath(); } } diff --git a/Service/Anonymize/Anonymizer/AlphaLower.php b/Service/Anonymize/Anonymizer/AlphaLower.php index a28df5c..65e073f 100644 --- a/Service/Anonymize/Anonymizer/AlphaLower.php +++ b/Service/Anonymize/Anonymizer/AlphaLower.php @@ -13,9 +13,6 @@ final class AlphaLower implements AnonymizerInterface { - /** - * Constants for alpha lower anonymizer - */ private const DEFAULT_LENGTH = 5; /** diff --git a/Service/Anonymize/Anonymizer/AlphaNum.php b/Service/Anonymize/Anonymizer/AlphaNum.php index 5e2f60f..cb47906 100644 --- a/Service/Anonymize/Anonymizer/AlphaNum.php +++ b/Service/Anonymize/Anonymizer/AlphaNum.php @@ -13,9 +13,6 @@ final class AlphaNum implements AnonymizerInterface { - /** - * Constants for alpha numeric anonymizer - */ private const DEFAULT_LENGTH = 5; /** diff --git a/Service/Anonymize/Anonymizer/AlphaUpper.php b/Service/Anonymize/Anonymizer/AlphaUpper.php index daf0343..88c1570 100644 --- a/Service/Anonymize/Anonymizer/AlphaUpper.php +++ b/Service/Anonymize/Anonymizer/AlphaUpper.php @@ -13,9 +13,6 @@ final class AlphaUpper implements AnonymizerInterface { - /** - * Constants for alpha upper anonymizer - */ private const DEFAULT_LENGTH = 5; /** diff --git a/Service/Anonymize/Anonymizer/Anonymous.php b/Service/Anonymize/Anonymizer/Anonymous.php index edc4b74..7b3863d 100644 --- a/Service/Anonymize/Anonymizer/Anonymous.php +++ b/Service/Anonymize/Anonymizer/Anonymous.php @@ -14,9 +14,6 @@ final class Anonymous implements AnonymizerInterface { - /** - * Constants for anonymous anonymizer - */ private const PHRASE = '%1Anonymous%2'; private const PREFIX_LENGTH = 3; private const SUFFIX_LENGTH = 2; diff --git a/Service/Anonymize/Anonymizer/Date.php b/Service/Anonymize/Anonymizer/Date.php index dbc4e9e..4496313 100644 --- a/Service/Anonymize/Anonymizer/Date.php +++ b/Service/Anonymize/Anonymizer/Date.php @@ -14,9 +14,6 @@ final class Date implements AnonymizerInterface { - /** - * Constants for date anonymizer - */ private const MIN_TIMESTAMP = 0; private const MAX_TIMESTAMP = 1557480188; diff --git a/Service/Anonymize/Anonymizer/Email.php b/Service/Anonymize/Anonymizer/Email.php index 77c9eb9..9f8103e 100644 --- a/Service/Anonymize/Anonymizer/Email.php +++ b/Service/Anonymize/Anonymizer/Email.php @@ -14,9 +14,6 @@ final class Email implements AnonymizerInterface { - /** - * Constants for value anonymizer - */ private const PHRASE = '%1-anonymous-%2@gdpr.org'; private const PREFIX_LENGTH = 3; private const SUFFIX_LENGTH = 2; diff --git a/Service/Anonymize/Anonymizer/Phone.php b/Service/Anonymize/Anonymizer/Phone.php index f9a87b8..21e0e34 100644 --- a/Service/Anonymize/Anonymizer/Phone.php +++ b/Service/Anonymize/Anonymizer/Phone.php @@ -11,9 +11,6 @@ final class Phone implements AnonymizerInterface { - /** - * Constants for phone number anonymizer - */ private const PHONE_NUMBER = '9999999999'; public function anonymize($value): string diff --git a/etc/di.xml b/etc/di.xml index f981193..3ffb624 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -1430,7 +1430,7 @@ Opengento\Gdpr\Model\Archive\Zip - + Opengento\Gdpr\Model\Archive\MoveToZip