Skip to content

Commit

Permalink
Fix customer deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Flurrybox committed Aug 15, 2019
1 parent 1da1fdb commit 31ff4b9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
28 changes: 4 additions & 24 deletions Cron/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

use Exception;
use Flurrybox\EnhancedPrivacy\Api\CustomerManagementInterface;
use Flurrybox\EnhancedPrivacy\Api\Data\ReasonInterfaceFactory;
use Flurrybox\EnhancedPrivacy\Api\Data\ScheduleInterface;
use Flurrybox\EnhancedPrivacy\Api\ReasonRepositoryInterface;
use Flurrybox\EnhancedPrivacy\Api\ScheduleRepositoryInterface;
use Flurrybox\EnhancedPrivacy\Helper\Data as PrivacyHelper;
use Magento\Customer\Api\CustomerRepositoryInterface;
Expand Down Expand Up @@ -69,16 +67,6 @@ class Schedule
*/
protected $customerRepository;

/**
* @var ReasonInterfaceFactory
*/
protected $reasonFactory;

/**
* @var ReasonRepositoryInterface
*/
protected $reasonRepository;

/**
* @var ResourceConnection
*/
Expand All @@ -103,8 +91,6 @@ class Schedule
* @param SearchCriteriaBuilder $criteriaBuilder
* @param CustomerManagementInterface $customerManagement
* @param CustomerRepositoryInterface $customerRepository
* @param ReasonInterfaceFactory $reasonFactory
* @param ReasonRepositoryInterface $reasonRepository
* @param ResourceConnection $resourceConnection
* @param LoggerInterface $logger
* @param ManagerInterface $eventManager
Expand All @@ -116,8 +102,6 @@ public function __construct(
SearchCriteriaBuilder $criteriaBuilder,
CustomerManagementInterface $customerManagement,
CustomerRepositoryInterface $customerRepository,
ReasonInterfaceFactory $reasonFactory,
ReasonRepositoryInterface $reasonRepository,
ResourceConnection $resourceConnection,
LoggerInterface $logger,
ManagerInterface $eventManager
Expand All @@ -128,8 +112,6 @@ public function __construct(
$this->criteriaBuilder = $criteriaBuilder;
$this->customerManagement = $customerManagement;
$this->customerRepository = $customerRepository;
$this->reasonFactory = $reasonFactory;
$this->reasonRepository = $reasonRepository;
$this->resourceConnection = $resourceConnection;
$this->logger = $logger;
$this->eventManager = $eventManager;
Expand Down Expand Up @@ -165,11 +147,9 @@ public function execute()

$this->processCustomer($schedule, $customer);

$reason = $this->reasonFactory->create();
$reason->setReason($schedule->getReason());

$this->reasonRepository->save($reason);
$this->scheduleRepository->delete($schedule);

$this->resourceConnection->getConnection()->commit();
} catch (Exception $e) {
$this->resourceConnection->getConnection()->rollBack();
$this->logger->error($e->getMessage());
Expand All @@ -189,11 +169,11 @@ public function execute()
protected function processCustomer(ScheduleInterface $schedule, CustomerInterface $customer)
{
switch ($schedule->getType()) {
case ScheduleInterface::TYPE_DELETE:
case PrivacyHelper::SCHEDULE_TYPE_DELETE:
$this->customerManagement->deleteCustomer($customer);
break;

case ScheduleInterface::TYPE_ANONYMIZE:
case PrivacyHelper::SCHEDULE_TYPE_ANONYMIZE:
$this->customerManagement->anonymizeCustomer($customer);
break;

Expand Down
14 changes: 13 additions & 1 deletion Privacy/Delete/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Magento\Eav\Model\Entity\Attribute\Source\Boolean;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Framework\Math\Random;
use Magento\Framework\Registry;

/**
* Process customer data.
Expand All @@ -48,21 +49,29 @@ class Customer implements DataDeleteInterface
*/
protected $random;

/**
* @var Registry
*/
protected $registry;

/**
* CustomerData constructor.
*
* @param CustomerRepositoryInterface $customerRepository
* @param EncryptorInterface $encryptor
* @param Random $random
* @param Registry $registry
*/
public function __construct(
CustomerRepositoryInterface $customerRepository,
EncryptorInterface $encryptor,
Random $random
Random $random,
Registry $registry
) {
$this->customerRepository = $customerRepository;
$this->encryptor = $encryptor;
$this->random = $random;
$this->registry = $registry;
}

/**
Expand All @@ -76,6 +85,9 @@ public function __construct(
*/
public function delete(CustomerInterface $customer)
{
/** @see \Magento\Framework\Model\ActionValidator\RemoveAction::isAllowed() **/
$this->registry->register('isSecureArea', true);

$this->customerRepository->deleteById($customer->getId());
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "flurrybox/enhanced-privacy",
"description": "Magento 2 module to make your store GDPR compatible",
"type": "magento2-module",
"version": "2.0.0",
"version": "2.1.0",
"authors": [
{
"name": "Flurrybox",
Expand Down

0 comments on commit 31ff4b9

Please sign in to comment.