Skip to content

Commit

Permalink
opengento#30 & minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Sep 26, 2019
1 parent 55fbcba commit 487667b
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 125 deletions.
2 changes: 1 addition & 1 deletion Api/Data/ExportEntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 0 additions & 3 deletions Console/Command/EraseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
3 changes: 0 additions & 3 deletions Console/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
5 changes: 4 additions & 1 deletion Model/Archive/MoveToArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion Model/Archive/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion Model/Config/Source/EraseComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -66,7 +68,7 @@ private function retrieveDelegateProcessors(): array
}
}

return \array_keys(\array_merge(...$delegateProcessors));
return array_keys(array_merge(...$delegateProcessors));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Model/Config/Source/VirtualArrayArgumentList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)];
}
}
Expand Down
41 changes: 16 additions & 25 deletions Model/Config/Source/VirtualCustomerAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion Model/Config/Source/VirtualEntityAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Framework\Data\OptionSourceInterface;
use Magento\Framework\Model\EntitySnapshot\AttributeProviderInterface;
use function array_keys;

/**
* Class VirtualEntityAttributes
Expand Down Expand Up @@ -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];
}
}
Expand Down
4 changes: 2 additions & 2 deletions Model/Entity/DataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion Model/Entity/DataCollectorGeneric.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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

namespace Opengento\Gdpr\Model\Export;

use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\NotFoundException;
use Opengento\Gdpr\Api\Data\ExportEntityInterface;
use Opengento\Gdpr\Model\Archive\MoveToArchive;
use Opengento\Gdpr\Model\Config;
use Opengento\Gdpr\Service\Export\ProcessorFactory;
use Opengento\Gdpr\Service\Export\RendererFactory;
use function sha1;
use const DIRECTORY_SEPARATOR;

final class ExportToFile
{
/**
* @var ProcessorFactory
*/
private $exportProcessorFactory;

/**
* @var RendererFactory
*/
private $exportRendererFactory;

/**
* @var MoveToArchive
*/
private $archive;

/**
* @var Config
*/
private $config;

public function __construct(
ProcessorFactory $exportProcessorFactory,
RendererFactory $exportRendererFactory,
MoveToArchive $archive,
Config $config
) {
$this->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();
}
}
2 changes: 1 addition & 1 deletion Model/ExportEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
70 changes: 8 additions & 62 deletions Model/ExportEntityManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@

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;
use Opengento\Gdpr\Api\Data\ExportEntityInterfaceFactory;
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
{
Expand All @@ -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
Expand All @@ -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;
}

Expand All @@ -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();
}
}
Loading

0 comments on commit 487667b

Please sign in to comment.