|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace CommerceLeague\ActiveCampaign\MessageQueue\Customer; |
|
7
|
|
|
|
|
8
|
|
|
use CommerceLeague\ActiveCampaign\Api\CustomerRepositoryInterface; |
|
9
|
|
|
use CommerceLeague\ActiveCampaign\Api\Data\CustomerInterface; |
|
10
|
|
|
use CommerceLeague\ActiveCampaign\Gateway\Client; |
|
11
|
|
|
use CommerceLeague\ActiveCampaign\Gateway\Request\CustomerBuilder as CustomerRequestBuilder; |
|
12
|
|
|
use CommerceLeague\ActiveCampaign\Logger\Logger; |
|
13
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\ConsumerInterface; |
|
14
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\HttpException; |
|
15
|
|
|
use Magento\Customer\Api\CustomerRepositoryInterface as MagentoCustomerRepositoryInterface; |
|
16
|
|
|
use Magento\Framework\Exception\CouldNotSaveException; |
|
17
|
|
|
use Magento\Framework\Exception\LocalizedException; |
|
18
|
|
|
use Magento\Framework\Exception\NoSuchEntityException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class ExportCustomerConsumer |
|
22
|
|
|
*/ |
|
23
|
|
|
class ExportCustomerConsumer implements ConsumerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var MagentoCustomerRepositoryInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $magentoCustomerRepository; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Logger |
|
32
|
|
|
*/ |
|
33
|
|
|
private $logger; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var CustomerRepositoryInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $customerRepository; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var CustomerRequestBuilder |
|
42
|
|
|
*/ |
|
43
|
|
|
private $customerRequestBuilder; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var Client |
|
47
|
|
|
*/ |
|
48
|
|
|
private $client; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param MagentoCustomerRepositoryInterface $magentoCustomerRepository |
|
52
|
|
|
* @param Logger $logger |
|
53
|
|
|
* @param CustomerRepositoryInterface $customerRepository |
|
54
|
|
|
* @param CustomerRequestBuilder $customerRequestBuilder |
|
55
|
|
|
* @param Client $client |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct( |
|
58
|
|
|
MagentoCustomerRepositoryInterface $magentoCustomerRepository, |
|
59
|
|
|
Logger $logger, |
|
60
|
|
|
CustomerRepositoryInterface $customerRepository, |
|
61
|
|
|
CustomerRequestBuilder $customerRequestBuilder, |
|
62
|
|
|
Client $client |
|
63
|
|
|
) { |
|
64
|
|
|
$this->magentoCustomerRepository = $magentoCustomerRepository; |
|
65
|
|
|
$this->logger = $logger; |
|
66
|
|
|
$this->customerRepository = $customerRepository; |
|
67
|
|
|
$this->customerRequestBuilder = $customerRequestBuilder; |
|
68
|
|
|
$this->client = $client; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $message |
|
73
|
|
|
* @throws CouldNotSaveException |
|
74
|
|
|
*/ |
|
75
|
|
|
public function consume(string $message): void |
|
76
|
|
|
{ |
|
77
|
|
|
$message = json_decode($message, true); |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
|
|
$magentoCustomer = $this->magentoCustomerRepository->getById($message['magento_customer_id']); |
|
81
|
|
|
} catch (NoSuchEntityException|LocalizedException $e) { |
|
82
|
|
|
$this->logger->error($e->getMessage()); |
|
83
|
|
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$customer = $this->customerRepository->getOrCreateByMagentoCustomerId($magentoCustomer->getId()); |
|
87
|
|
|
$request = $this->customerRequestBuilder->build($magentoCustomer); |
|
88
|
|
|
|
|
89
|
|
|
try { |
|
90
|
|
|
$apiResponse = $this->performApiRequest($customer, $request); |
|
91
|
|
|
} catch (HttpException $e) { |
|
92
|
|
|
$this->logger->error($e->getMessage()); |
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$customer->setActiveCampaignId($apiResponse['ecomCustomer']['id']); |
|
97
|
|
|
$this->customerRepository->save($customer); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param CustomerInterface $customer |
|
102
|
|
|
* @param array $request |
|
103
|
|
|
* @return array |
|
104
|
|
|
* @throws HttpException |
|
105
|
|
|
*/ |
|
106
|
|
|
private function performApiRequest(CustomerInterface $customer, array $request): array |
|
107
|
|
|
{ |
|
108
|
|
|
if ($activeCampaignId = $customer->getActiveCampaignId()) { |
|
109
|
|
|
return $this->client->getCustomerApi()->update((int)$activeCampaignId, ['ecomCustomer' => $request]); |
|
110
|
|
|
} else { |
|
111
|
|
|
return $this->client->getCustomerApi()->create(['ecomCustomer' => $request]); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|