/**
  * Execute method.
  *
  * @param \Magento\Framework\Event\Observer $observer
  *
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $order = $observer->getEvent()->getOrder();
     $email = $order->getCustomerEmail();
     $website = $this->storeManager->getWebsite($order->getWebsiteId());
     $storeName = $this->storeManager->getStore($order->getStoreId())->getName();
     //if api is not enabled
     if (!$this->helper->isEnabled($website)) {
         return $this;
     }
     //automation enrolment for order
     if ($order->getCustomerIsGuest()) {
         // guest to automation mapped
         $programType = 'XML_PATH_CONNECTOR_AUTOMATION_STUDIO_GUEST_ORDER';
         $automationType = \Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_TYPE_NEW_GUEST_ORDER;
     } else {
         // customer to automation mapped
         $programType = 'XML_PATH_CONNECTOR_AUTOMATION_STUDIO_ORDER';
         $automationType = \Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_TYPE_NEW_ORDER;
     }
     $programId = $this->helper->getAutomationIdByType($programType, $order->getWebsiteId());
     //the program is not mapped
     if (!$programId) {
         return $this;
     }
     try {
         $this->automationFactory->create()->setEmail($email)->setAutomationType($automationType)->setEnrolmentStatus(\Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_STATUS_PENDING)->setTypeId($order->getId())->setWebsiteId($website->getId())->setStoreName($storeName)->setProgramId($programId)->save();
     } catch (\Exception $e) {
         $this->helper->debug((string) $e, []);
     }
     return $this;
 }
 /**
  * Save/reset the order as transactional data.
  *
  * @param \Magento\Framework\Event\Observer $observer
  *
  * @return $this
  *
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     try {
         $order = $observer->getEvent()->getOrder();
         $status = $order->getStatus();
         $storeId = $order->getStoreId();
         $store = $this->storeManager->getStore($storeId);
         $storeName = $store->getName();
         $websiteId = $store->getWebsiteId();
         $customerEmail = $order->getCustomerEmail();
         // start app emulation
         $appEmulation = $this->emulationFactory->create();
         $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
         $emailOrder = $this->emailOrderFactory->create()->loadByOrderId($order->getEntityId(), $order->getQuoteId());
         //reimport email order
         $emailOrder->setUpdatedAt($order->getUpdatedAt())->setCreatedAt($order->getUpdatedAt())->setStoreId($storeId)->setOrderStatus($status);
         if ($emailOrder->getEmailImported() != \Dotdigitalgroup\Email\Model\Contact::EMAIL_CONTACT_IMPORTED) {
             $emailOrder->setEmailImported(null);
         }
         //if api is not enabled
         if (!$store->getWebsite()->getConfig(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_API_ENABLED)) {
             return $this;
         }
         // check for order status change
         $statusBefore = $this->registry->registry('sales_order_status_before');
         if ($status != $statusBefore) {
             //If order status has changed and order is already imported then set modified to 1
             if ($emailOrder->getEmailImported() == \Dotdigitalgroup\Email\Model\Contact::EMAIL_CONTACT_IMPORTED) {
                 $emailOrder->setModified(\Dotdigitalgroup\Email\Model\Contact::EMAIL_CONTACT_IMPORTED);
             }
         }
         // set back the current store
         $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
         $emailOrder->save();
         //@codingStandardsIgnoreStart
         //Status check automation enrolment
         $configStatusAutomationMap = unserialize($this->scopeConfig->getValue(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_AUTOMATION_STUDIO_ORDER_STATUS, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $order->getStore()));
         //@codingStandardsIgnoreEnd
         if (!empty($configStatusAutomationMap)) {
             foreach ($configStatusAutomationMap as $configMap) {
                 if ($configMap['status'] == $status) {
                     //send to automation queue
                     $this->doAutomationEnrolment(['programId' => $configMap['automation'], 'automationType' => 'order_automation_' . $status, 'email' => $customerEmail, 'order_id' => $order->getId(), 'website_id' => $websiteId, 'store_name' => $storeName]);
                 }
             }
         }
         //If customer's first order
         if ($order->getCustomerId()) {
             $orders = $this->orderCollectionFactory->create()->addFieldToFilter('customer_id', $order->getCustomerId());
             if ($orders->getSize() == 1) {
                 $automationTypeNewOrder = \Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_TYPE_CUSTOMER_FIRST_ORDER;
                 $programIdNewOrder = $this->helper->getAutomationIdByType('XML_PATH_CONNECTOR_AUTOMATION_STUDIO_FIRST_ORDER', $order->getWebsiteId());
                 //send to automation queue
                 $this->doAutomationEnrolment(['programId' => $programIdNewOrder, 'automationType' => $automationTypeNewOrder, 'email' => $customerEmail, 'order_id' => $order->getId(), 'website_id' => $websiteId, 'store_name' => $storeName]);
             }
         }
         //admin oder when editing the first one is canceled
         $this->registry->unregister('sales_order_status_before');
     } catch (\Exception $e) {
         throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
     }
     return $this;
 }