|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\TestFramework\Customer\Model; |
| 9 | + |
| 10 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 11 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 12 | +use Magento\Framework\Registry; |
| 13 | + |
| 14 | +/** |
| 15 | + * Delete customer by email or id |
| 16 | + */ |
| 17 | +class DeleteCustomer |
| 18 | +{ |
| 19 | + /** @var CustomerRepositoryInterface */ |
| 20 | + private $customerRepository; |
| 21 | + |
| 22 | + /** @var Registry */ |
| 23 | + private $registry; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param CustomerRepositoryInterface $customerRepository |
| 27 | + * @param Registry $registry |
| 28 | + */ |
| 29 | + public function __construct(CustomerRepositoryInterface $customerRepository, Registry $registry) |
| 30 | + { |
| 31 | + $this->customerRepository = $customerRepository; |
| 32 | + $this->registry = $registry; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Delete customer by id or email |
| 37 | + * |
| 38 | + * @param int|string $id |
| 39 | + * @return void |
| 40 | + */ |
| 41 | + public function execute($id): void |
| 42 | + { |
| 43 | + $isSecure = $this->registry->registry('isSecureArea'); |
| 44 | + |
| 45 | + $this->registry->unregister('isSecureArea'); |
| 46 | + $this->registry->register('isSecureArea', true); |
| 47 | + |
| 48 | + try { |
| 49 | + $customer = is_numeric($id) ? $this->customerRepository->getById($id) : $this->customerRepository->get($id); |
| 50 | + $this->customerRepository->delete($customer); |
| 51 | + } catch (NoSuchEntityException $e) { |
| 52 | + //customer already deleted |
| 53 | + } |
| 54 | + |
| 55 | + $this->registry->unregister('isSecureArea'); |
| 56 | + $this->registry->register('isSecureArea', $isSecure); |
| 57 | + } |
| 58 | +} |
0 commit comments