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 ExportContactConsumer |
15
|
|
|
*/ |
16
|
|
|
class ExportContactConsumer |
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
|
|
|
* @throws CouldNotSaveException |
51
|
|
|
*/ |
52
|
|
|
public function consume(string $message): void |
53
|
|
|
{ |
54
|
|
|
$message = json_decode($message, true); |
55
|
|
|
$contact = $this->contactRepository->getOrCreateByEmail($message['email']); |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
$apiResponse = $this->clientHelper->getContactApi()->upsert(['contact' => $message['request']]); |
59
|
|
|
} catch (HttpException $e) { |
60
|
|
|
$this->logger->error($e->getMessage()); |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$contact->setActiveCampaignId($apiResponse['contact']['id']); |
65
|
|
|
|
66
|
|
|
$this->contactRepository->save($contact); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|