/**
  * Export subscribers per website.
  *
  * @param $website
  *
  * @return int
  *
  * @throws LocalizedException
  */
 public function exportSubscribersPerWebsite($website)
 {
     $updated = 0;
     $limit = $this->helper->getSyncLimit($website->getId());
     //subscriber collection to import
     $subscribers = $this->contactFactory->create()->getSubscribersToImport($website, $limit);
     if ($subscribers->getSize()) {
         $subscribersFilename = strtolower($website->getCode() . '_subscribers_' . date('d_m_Y_Hi') . '.csv');
         //get mapped storename
         $subscriberStoreName = $this->helper->getMappedStoreName($website);
         //file headers
         $this->file->outputCSV($this->file->getFilePath($subscribersFilename), ['Email', 'emailType', $subscriberStoreName]);
         $emails = $subscribers->getColumnValues('email');
         $subscriberFactory = $this->subscriberFactory->create();
         $subscribersData = $subscriberFactory->getCollection()->addFieldToFilter('subscriber_email', ['in' => $emails])->addFieldToSelect(['subscriber_email', 'store_id'])->toArray();
         foreach ($subscribers as $subscriber) {
             $email = $subscriber->getEmail();
             $storeId = $this->getStoreIdForSubscriber($email, $subscribersData['items']);
             $storeName = $this->storeManager->getStore($storeId)->getName();
             // save data for subscribers
             $this->file->outputCSV($this->file->getFilePath($subscribersFilename), [$email, 'Html', $storeName]);
             //@codingStandardsIgnoreStart
             $subscriber->setSubscriberImported(1)->save();
             //@codingStandardsIgnoreEnd
             ++$updated;
         }
         $this->helper->log('Subscriber filename: ' . $subscribersFilename);
         //register in queue with importer
         $this->importerFactory->create()->registerQueue(\Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_SUBSCRIBERS, '', \Dotdigitalgroup\Email\Model\Importer::MODE_BULK, $website->getId(), $subscribersFilename);
     }
     //add updated number for the website
     $this->countSubscriber += $updated;
     return $updated;
 }
 /**
  * @param \Magento\Store\Api\Data\WebsiteInterface $website
  *
  * @return int
  */
 public function exportCustomersForWebsite(\Magento\Store\Api\Data\WebsiteInterface $website)
 {
     $allMappedHash = [];
     //admin sync limit of batch size for contacts
     $syncLimit = $this->helper->getSyncLimit($website);
     //address book id mapped
     $customerAddressBook = $this->helper->getCustomerAddressBook($website);
     //skip website if address book not mapped
     if (!$customerAddressBook) {
         return 0;
     }
     $connection = $this->resource->getConnection();
     //contacts ready for website
     $contacts = $this->contactCollection->create()->addFieldToSelect('*')->addFieldToFilter('email_imported', ['null' => true])->addFieldToFilter('customer_id', ['neq' => '0'])->addFieldToFilter('website_id', $website->getId())->setPageSize($syncLimit);
     // no contacts found
     if (!$contacts->getSize()) {
         return 0;
     }
     //customer filename
     $customersFile = strtolower($website->getCode() . '_customers_' . date('d_m_Y_Hi') . '.csv');
     $this->helper->log('Customers file : ' . $customersFile);
     //get customers ids
     $customerIds = $contacts->getColumnValues('customer_id');
     /*
      * HEADERS.
      */
     $mappedHash = $this->helper->getWebsiteCustomerMappingDatafields($website);
     $headers = $mappedHash;
     //custom customer attributes
     $customAttributes = $this->helper->getCustomAttributes($website);
     if ($customAttributes) {
         foreach ($customAttributes as $data) {
             $headers[] = $data['datafield'];
             $allMappedHash[$data['attribute']] = $data['datafield'];
         }
     }
     $headers[] = 'Email';
     $headers[] = 'EmailType';
     $this->file->outputCSV($this->file->getFilePath($customersFile), $headers);
     /*
      * END HEADERS.
      */
     //customer collection
     $customerCollection = $this->_getCustomerCollection($customerIds, $website->getId());
     $countIds = [];
     foreach ($customerCollection as $customer) {
         $connectorCustomer = $this->emailCustomer->create();
         $connectorCustomer->setMappingHash($mappedHash);
         $connectorCustomer->setCustomerData($customer);
         //count number of customers
         $countIds[] = $customer->getId();
         if ($connectorCustomer) {
             foreach ($customAttributes as $data) {
                 $attribute = $data['attribute'];
                 $value = $customer->getData($attribute);
                 $connectorCustomer->setData($value);
             }
         }
         //contact email and email type
         $connectorCustomer->setData($customer->getEmail());
         $connectorCustomer->setData('Html');
         // save csv file data for customers
         $this->file->outputCSV($this->file->getFilePath($customersFile), $connectorCustomer->toCSVArray());
         //clear collection and free memory
         $customer->clearInstance();
     }
     $customerNum = count($customerIds);
     $this->helper->log('Website : ' . $website->getName() . ', customers = ' . $customerNum);
     $this->helper->log('---------------------------- execution time :' . gmdate('H:i:s', microtime(true) - $this->start));
     //file was created - continue for queue the export
     //@codingStandardsIgnoreStart
     if (is_file($this->file->getFilePath($customersFile))) {
         //@codingStandardsIgnoreEnd
         if ($customerNum > 0) {
             //register in queue with importer
             $this->importerFactory->create()->registerQueue(\Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_CONTACT, '', \Dotdigitalgroup\Email\Model\Importer::MODE_BULK, $website->getId(), $customersFile);
             //set imported
             $tableName = $this->resource->getTableName('email_contact');
             $ids = implode(', ', $customerIds);
             $connection->update($tableName, ['email_imported' => 1], "customer_id IN ({$ids})");
         }
     }
     $this->countCustomers += $customerNum;
     return $customerNum;
 }