1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
*/ |
5
|
|
|
namespace CommerceLeague\ActiveCampaign\MessageQueue; |
6
|
|
|
|
7
|
|
|
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface; |
8
|
|
|
use CommerceLeague\ActiveCampaign\Helper\Client as ClientHelper; |
9
|
|
|
use CommerceLeague\ActiveCampaign\Logger\Logger; |
10
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\HttpException; |
11
|
|
|
use Magento\Framework\Exception\CouldNotSaveException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class CreateUpdateConsumer |
15
|
|
|
*/ |
16
|
|
|
class SyncContactConsumer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ContactRepositoryInterface |
20
|
|
|
*/ |
21
|
|
|
private $contactRepository; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Logger |
25
|
|
|
*/ |
26
|
|
|
private $logger; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ClientHelper |
30
|
|
|
*/ |
31
|
|
|
private $clientHelper; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ContactRepositoryInterface $contactRepository |
35
|
|
|
* @param Logger $logger |
36
|
|
|
* @param ClientHelper $clientHelper |
37
|
|
|
*/ |
38
|
|
|
public function __construct( |
39
|
|
|
ContactRepositoryInterface $contactRepository, |
40
|
|
|
Logger $logger, |
41
|
|
|
ClientHelper $clientHelper |
42
|
|
|
) { |
43
|
|
|
$this->contactRepository = $contactRepository; |
44
|
|
|
$this->logger = $logger; |
45
|
|
|
$this->clientHelper = $clientHelper; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $message |
50
|
|
|
*/ |
51
|
|
|
public function consume(string $message): void |
52
|
|
|
{ |
53
|
|
|
$message = json_decode($message, true); |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$contact = $this->contactRepository->getOrCreateByEmail($message['email']); |
57
|
|
|
} catch (CouldNotSaveException $e) { |
58
|
|
|
$this->logger->error(__('Unable to find contact with email "%1".', $message['email'])); |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
try { |
63
|
|
|
$apiResponse = $this->clientHelper->getContactApi()->upsert(['contact' => $message['request']]); |
64
|
|
|
} catch (HttpException $e) { |
65
|
|
|
$this->logger->error($e->getMessage()); |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$contact->setActiveCampaignId($apiResponse['contact']['id']); |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
$this->contactRepository->save($contact); |
73
|
|
|
} catch (CouldNotSaveException $e) { |
74
|
|
|
$this->logger->error($e->getMessage()); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|