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