Skip to content

Commit

Permalink
Fix #109 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Sep 26, 2021
1 parent 8533f97 commit 79a5f7b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
33 changes: 21 additions & 12 deletions Model/Customer/Anonymize/Processor/CustomerDataProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use DateTime;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\SessionCleanerInterface;
use Magento\Customer\Model\CustomerRegistry;
use Magento\Framework\Api\SearchCriteriaBuilder;
Expand All @@ -22,6 +23,7 @@
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Store\Model\ScopeInterface;
use Opengento\Gdpr\Model\Customer\OrigDataRegistry;
use Opengento\Gdpr\Service\Anonymize\AnonymizerInterface;
use Opengento\Gdpr\Service\Erase\ProcessorInterface;
use function mt_rand;
Expand Down Expand Up @@ -58,6 +60,11 @@ final class CustomerDataProcessor implements ProcessorInterface
*/
private $customerRegistry;

/**
* @var OrigDataRegistry
*/
private $origDataRegistry;

/**
* @var SessionCleanerInterface
*/
Expand All @@ -74,6 +81,7 @@ public function __construct(
OrderRepositoryInterface $orderRepository,
SearchCriteriaBuilder $criteriaBuilder,
CustomerRegistry $customerRegistry,
OrigDataRegistry $origDataRegistry,
SessionCleanerInterface $sessionCleaner,
ScopeConfigInterface $scopeConfig
) {
Expand All @@ -82,6 +90,7 @@ public function __construct(
$this->orderRepository = $orderRepository;
$this->criteriaBuilder = $criteriaBuilder;
$this->customerRegistry = $customerRegistry;
$this->origDataRegistry = $origDataRegistry;
$this->sessionCleaner = $sessionCleaner;
$this->scopeConfig = $scopeConfig;
}
Expand All @@ -95,11 +104,14 @@ public function execute(int $customerId): bool
$isRemoved = false;

try {
if ($this->shouldRemoveCustomerWithoutOrders() && !$this->fetchOrdersList($customerId)->getTotalCount()) {
$isRemoved = $this->customerRepository->deleteById($customerId);
$customer = $this->customerRepository->getById($customerId);
$this->origDataRegistry->set(clone $customer);

if ($this->shouldRemoveCustomerWithoutOrders() && !$this->fetchOrdersList($customer)->getTotalCount()) {
$isRemoved = $this->customerRepository->deleteById($customer->getId());
}
if (!$isRemoved) {
$this->anonymizeCustomer($customerId);
$this->anonymizeCustomer($customer);
}
} catch (NoSuchEntityException $e) {
return false;
Expand All @@ -110,32 +122,29 @@ public function execute(int $customerId): bool
return true;
}

private function fetchOrdersList(int $customerId): OrderSearchResultInterface
private function fetchOrdersList(CustomerInterface $customer): OrderSearchResultInterface
{
$this->criteriaBuilder->addFilter(OrderInterface::CUSTOMER_ID, $customerId);
$this->criteriaBuilder->addFilter(OrderInterface::CUSTOMER_ID, $customer->getId());

return $this->orderRepository->getList($this->criteriaBuilder->create());
}

/**
* @param int $customerId
* @throws LocalizedException
* @throws NoSuchEntityException
* @throws InputException
* @throws InputMismatchException
*/
private function anonymizeCustomer(int $customerId): void
private function anonymizeCustomer(CustomerInterface $customer): void
{
$this->customerRegistry->remove($customerId);
$this->customerRegistry->remove($customer->getId());

$secureData = $this->customerRegistry->retrieveSecureData($customerId);
$secureData = $this->customerRegistry->retrieveSecureData($customer->getId());
$dateTime = (new DateTime())->setTimestamp(PHP_INT_MAX);
$secureData->setData('lock_expires', $dateTime->format(DateTimeFormat::DATETIME_PHP_FORMAT));
$secureData->setPasswordHash(sha1(uniqid((string) mt_rand(), true)));

$this->customerRepository->save(
$this->anonymizer->anonymize($this->customerRepository->getById($customerId))
);
$this->customerRepository->save($this->anonymizer->anonymize($customer));
}

private function shouldRemoveCustomerWithoutOrders(): bool
Expand Down
14 changes: 6 additions & 8 deletions Model/Customer/Erase/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

namespace Opengento\Gdpr\Model\Customer\Erase;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Opengento\Gdpr\Api\Data\EraseEntityInterface;
use Opengento\Gdpr\Model\Customer\Notifier\SenderInterface;
use Opengento\Gdpr\Model\Customer\OrigDataRegistry;
use Opengento\Gdpr\Model\Erase\NotifierInterface;

final class Notifier implements NotifierInterface
Expand All @@ -21,25 +20,24 @@ final class Notifier implements NotifierInterface
private $senders;

/**
* @var CustomerRepositoryInterface
* @var OrigDataRegistry
*/
private $customerRepository;
private $origDataRegistry;

public function __construct(
array $senders,
CustomerRepositoryInterface $customerRepository
OrigDataRegistry $origDataRegistry
) {
$this->senders = $senders;
$this->customerRepository = $customerRepository;
$this->origDataRegistry = $origDataRegistry;
}

/**
* @inheritdoc
* @throws LocalizedException
*/
public function notify(EraseEntityInterface $eraseEntity): void
{
$customer = $this->customerRepository->getById($eraseEntity->getEntityId());
$customer = $this->origDataRegistry->get($eraseEntity->getEntityId());

foreach ($this->senders as $sender) {
$sender->send($customer);
Expand Down
28 changes: 28 additions & 0 deletions Model/Customer/OrigDataRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Gdpr\Model\Customer;

use Magento\Customer\Api\Data\CustomerInterface;

final class OrigDataRegistry
{
/**
* @var CustomerInterface[]
*/
private $customers = [];

public function get(int $customerId): CustomerInterface
{
return $this->customers[$customerId];
}

public function set(CustomerInterface $customer): void
{
$this->customers[(int) $customer->getId()] = $customer;
}
}

0 comments on commit 79a5f7b

Please sign in to comment.