/** * @param array $records * @throws \Enlight_Event_Exception * @throws \Exception * @throws \Zend_Db_Adapter_Exception */ public function write($records) { $customerCount = 0; if (empty($records)) { $message = SnippetsHelper::getNamespace()->get('adapters/customer/no_records', 'No customer records were found.'); throw new \Exception($message); } $records = $this->eventManager->filter('Shopware_Components_SwagImportExport_DbAdapters_CustomerDbAdapter_Write', $records, ['subject' => $this]); $defaultValues = $this->getDefaultValues(); foreach ($records['default'] as $record) { try { $customerCount++; $record = $this->validator->filterEmptyString($record); $customer = $this->findExistingEntries($record); $createNewCustomer = false; if (!$customer instanceof Customer) { $createNewCustomer = true; $record = $this->dataManager->setDefaultFieldsForCreate($record, $defaultValues); $this->validator->checkRequiredFieldsForCreate($record); $customer = new Customer(); } $this->preparePassword($record); $this->validator->checkRequiredFields($record); $this->validator->validate($record, CustomerDataType::$mapper); $customerData = $this->prepareCustomer($record); $customerData['billing'] = $this->prepareBilling($record); $customerData['shipping'] = $this->prepareShipping($record, $createNewCustomer, $customerData['billing']); $customer->fromArray($customerData); if (isset($customerData['subshopID'])) { $shop = $this->manager->getRepository(Shop::class)->find($customerData['subshopID']); if (!$shop) { $message = SnippetsHelper::getNamespace()->get('adapters/shop_not_found', 'Shop with id %s was not found'); throw new AdapterException(sprintf($message, $customerData['subshopID'])); } $customer->setShop($shop); } if (isset($customerData['languageId'])) { $languageSubShop = $this->manager->getRepository(Shop::class)->find($customerData['languageId']); if (!$languageSubShop) { $message = SnippetsHelper::getNamespace()->get('adapters/language_shop_not_found', 'Language-Shop with id %s was not found'); throw new AdapterException(sprintf($message, $customerData['languageId'])); } $customer->setLanguageSubShop($languageSubShop); } $billing = $customer->getDefaultBillingAddress(); if (!$billing instanceof Address) { $billing = new Address(); $billing->setCustomer($customer); } if (isset($customerData['billing']['countryId'])) { $customerData['billing']['country'] = $this->manager->find(Country::class, $customerData['billing']['countryId']); } if (isset($customerData['billing']['stateId'])) { $customerData['billing']['state'] = $this->manager->find(State::class, $customerData['billing']['stateId']); } $billing->fromArray($customerData['billing']); $shipping = $customer->getDefaultShippingAddress(); if (!$shipping instanceof Address) { $shipping = new Address(); $shipping->setCustomer($customer); } if (isset($customerData['shipping']['countryId'])) { $customerData['shipping']['country'] = $this->manager->find(Country::class, $customerData['shipping']['countryId']); } if (isset($customerData['shipping']['stateId'])) { $customerData['shipping']['state'] = $this->manager->find(State::class, $customerData['shipping']['stateId']); } $shipping->fromArray($customerData['shipping']); $customer->setFirstname($billing->getFirstname()); $customer->setLastname($billing->getLastname()); $customer->setSalutation($billing->getSalutation()); $customer->setTitle($billing->getTitle()); $violations = $this->manager->validate($customer); if ($violations->count() > 0) { $message = SnippetsHelper::getNamespace()->get('adapters/customer/no_valid_customer_entity', 'No valid user entity for email %s'); $message = sprintf($message, $customer->getEmail()); foreach ($violations as $violation) { $message .= "\n" . $violation->getPropertyPath() . ': ' . $violation->getMessage(); } throw new AdapterException($message); } $this->manager->persist($customer); if ($createNewCustomer) { $this->manager->flush(); } $customer->setDefaultBillingAddress($billing); $this->manager->persist($billing); $customer->setDefaultShippingAddress($shipping); $this->manager->persist($shipping); $this->insertCustomerAttributes($customerData, $customer->getId(), $createNewCustomer); if ($customerCount % 20 === 0) { $this->manager->flush(); } } catch (AdapterException $e) { $message = $e->getMessage(); $this->saveMessage($message); } } $this->manager->flush(); }
public function test_validator_without_billing_country_id_throws_exception() { $customerValidator = new CustomerValidator(); $this->expectException(AdapterException::class); $this->expectExceptionMessage('Es muss eine Rechnungsland-ID für den Kunden mit der E-Mail validator@test.com übergeben werden.'); $customerValidator->checkRequiredFieldsForCreate(['email' => '*****@*****.**', 'unhashedPassword' => 'testABC', 'password' => '', 'encoder' => 'md5', 'customergroup' => 'EK', 'billingSalutation' => 'mr', 'billingFirstname' => 'Test', 'billingLastname' => 'User', 'billingStreet' => 'Test street 123', 'billingZipcode' => '12345', 'billingCity' => 'Test City']); }