-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCustomerRepository.php
58 lines (51 loc) · 1.5 KB
/
CustomerRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace Taxjar\SalesTax\Plugin\Customer\Model\ResourceModel;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Taxjar\SalesTax\Model\Client;
use Taxjar\SalesTax\Model\ClientFactory;
class CustomerRepository
{
/**
* @var ClientFactory
*/
private $clientFactory;
/**
* @param ClientFactory $clientFactory
*/
public function __construct(ClientFactory $clientFactory)
{
$this->clientFactory = $clientFactory;
}
/**
* Attempt to delete the corresponding TJ API resource prior to customer deletion
*
* @param CustomerRepositoryInterface $subject
* @param int $customerId
* @return int
*/
public function beforeDeleteById(
CustomerRepositoryInterface $subject,
$customerId
) {
try {
// Although not annotated, client resource methods may throw exception
$this->getClient()->deleteResource('customers', $customerId);
} catch (LocalizedException $e) {
$message = 'Could not delete customer #' . $customerId . ": " . $e->getMessage();
$this->logger->log($message, 'error');
}
return $customerId;
}
/**
* Returns new TJ client configured to show error responses.
*
* @return Client
*/
private function getClient(): Client
{
$client = $this->clientFactory->create();
$client->showResponseErrors(true);
return $client;
}
}