customerCollection = $customerCollection; $this->sourceCustomerRepository = $sourceCustomerRepository; $this->sourceFactory = $sourceFactory; $this->sourceModel = $sourceModel; $this->customerInterfaceFactory = $customerInterfaceFactory; $this->sourceNetsuiteSync = $sourceNetsuiteSync; parent::__construct(); } protected function configure() { $this->setName('customer:source:sync'); $this->setDescription('Add customer init data to inventory_source_customer table'); } /** * @param string $sourceName * @return SourceInterface */ protected function getSourceByName(string $sourceName) { $sourceName = trim($sourceName); if (isset($this->sources[$sourceName]) && $this->sources[$sourceName]->getSourceCode()) { return $this->sources[$sourceName]; } $source = $this->sourceFactory->create(); $this->sourceModel->load($source, $sourceName, 'name'); $this->sources[$sourceName] = $source; return $this->sources[$sourceName]; } protected function execute(InputInterface $input, OutputInterface $output) { $this->customerCollection->load(); foreach ($this->customerCollection as $customer) { /* @var $customer \Magento\Customer\Model\Customer */ $attribute = $customer->getAttribute('lts_office'); $name = $attribute->getSource()->getOptionText($customer->getLtsOffice()); try { try { $this->sourceCustomerRepository->getFromCustomerId($customer->getId()); $output->writeln(sprintf("customer %s, source name %s already exists in customer source.", $customer->getEmail(), $name)); } catch (NoSuchEntityException $e) { $source = $this->getSourceByName($name); $sourceCustomer = $this->customerInterfaceFactory->create(); $sourceCustomer->setCustomerId($customer->getId())->setSourceCode($source->getSourceCode()?? self::DEFAULT_SOURCE); $this->sourceCustomerRepository->save($sourceCustomer); $output->writeln(sprintf("customer %s, source name %s sync success.", $customer->getEmail(), $name)); } } catch (\Exception $e) { $output->writeln(sprintf("customer source sync fail with customer %s, source name %s . Exception message %s", $customer->getEmail(), $name, $e->getMessage())); } } return 0; } }