|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace CommerceLeague\ActiveCampaign\Observer\Customer; |
|
7
|
|
|
|
|
8
|
|
|
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface; |
|
9
|
|
|
use CommerceLeague\ActiveCampaign\Api\CustomerRepositoryInterface; |
|
10
|
|
|
use CommerceLeague\ActiveCampaign\Logger\Logger; |
|
11
|
|
|
use CommerceLeague\ActiveCampaign\Helper\Config as ConfigHelper; |
|
12
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\Customer\CreateMessageBuilder; |
|
13
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\Topics; |
|
14
|
|
|
use Magento\Customer\Model\Customer; |
|
15
|
|
|
use Magento\Framework\Event\Observer; |
|
16
|
|
|
use Magento\Framework\Event\ObserverInterface; |
|
17
|
|
|
use Magento\Framework\Exception\CouldNotSaveException; |
|
18
|
|
|
use Magento\Framework\MessageQueue\PublisherInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class CreateUpdateCustomerObserver |
|
22
|
|
|
*/ |
|
23
|
|
|
class CreateUpdateCustomerObserver implements ObserverInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var ConfigHelper |
|
27
|
|
|
*/ |
|
28
|
|
|
private $configHelper; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ContactRepositoryInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $customerRepository; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Logger |
|
37
|
|
|
*/ |
|
38
|
|
|
private $logger; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var CreateMessageBuilder |
|
42
|
|
|
*/ |
|
43
|
|
|
private $createMessageBuilder; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var PublisherInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
private $publisher; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param ConfigHelper $configHelper |
|
52
|
|
|
* @param CustomerRepositoryInterface $customerRepository |
|
53
|
|
|
* @param Logger $logger |
|
54
|
|
|
* @param CreateMessageBuilder $createMessageBuilder |
|
55
|
|
|
* @param PublisherInterface $publisher |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct( |
|
58
|
|
|
ConfigHelper $configHelper, |
|
59
|
|
|
CustomerRepositoryInterface $customerRepository, |
|
60
|
|
|
Logger $logger, |
|
61
|
|
|
CreateMessageBuilder $createMessageBuilder, |
|
62
|
|
|
PublisherInterface $publisher |
|
63
|
|
|
) { |
|
64
|
|
|
$this->configHelper = $configHelper; |
|
65
|
|
|
$this->customerRepository = $customerRepository; |
|
|
|
|
|
|
66
|
|
|
$this->logger = $logger; |
|
67
|
|
|
$this->createMessageBuilder = $createMessageBuilder; |
|
68
|
|
|
$this->publisher = $publisher; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritDoc |
|
73
|
|
|
*/ |
|
74
|
|
|
public function execute(Observer $observer) |
|
75
|
|
|
{ |
|
76
|
|
|
if (!$this->configHelper->isApiEnabled()) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** @var Customer $magentoCustomer */ |
|
81
|
|
|
$magentoCustomer = $observer->getEvent()->getData('customer'); |
|
82
|
|
|
|
|
83
|
|
|
try { |
|
84
|
|
|
$customer = $this->customerRepository->getOrCreateByMagentoCustomer($magentoCustomer); |
|
|
|
|
|
|
85
|
|
|
} catch (CouldNotSaveException $e) { |
|
86
|
|
|
$this->logger->critical($e); |
|
87
|
|
|
return; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ($customer->getActiveCampaignId()) { |
|
91
|
|
|
// TODO::publish update message |
|
92
|
|
|
} else { |
|
93
|
|
|
$this->publisher->publish( |
|
94
|
|
|
Topics::CUSTOMER_CREATE, |
|
95
|
|
|
$this->createMessageBuilder->build($customer, $magentoCustomer) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..