/**
  * @param EntityManager $manager
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     foreach ($this->addresses as $addressData) {
         $address = new AccountAddress();
         $address->setSystemOrganization($this->getOrganization($manager));
         $address->setFrontendOwner($this->getReference($addressData['account']));
         $this->addAddress($manager, $addressData, $address);
     }
     $manager->flush();
 }
 /**
  * @param Account $account
  * @param AccountAddress $address
  * @return array
  * @throws BadRequestHttpException
  */
 protected function update(Account $account, AccountAddress $address)
 {
     $responseData = ['saved' => false, 'entity' => $account];
     if ($this->getRequest()->getMethod() === 'GET' && !$address->getId() && !$account->getAddresses()->count()) {
         $address->setPrimary(true);
     }
     if (!$address->getFrontendOwner()) {
         $account->addAddress($address);
     } elseif ($address->getFrontendOwner()->getId() !== $account->getId()) {
         throw new BadRequestHttpException('Address must belong to Account');
     }
     $form = $this->createForm(AccountTypedAddressType::NAME, $address);
     $handler = new AddressHandler($form, $this->getRequest(), $this->getDoctrine()->getManagerForClass($this->container->getParameter('orob2b_account.entity.account_address.class')));
     if ($handler->process($address)) {
         $this->getDoctrine()->getManager()->flush();
         $responseData['entity'] = $address;
         $responseData['saved'] = true;
     }
     $responseData['form'] = $form->createView();
     $responseData['routes'] = ['create' => 'orob2b_account_address_create', 'update' => 'orob2b_account_address_update'];
     return $responseData;
 }
 /**
  * @return array
  */
 public function submitWithFormSubscribersProvider()
 {
     $accountAddress1 = new AccountAddress();
     $accountAddress1->setTypes(new ArrayCollection([$this->billingType, $this->shippingType]));
     $accountAddress2 = new AccountAddress();
     $accountAddress2->setTypes(new ArrayCollection([$this->billingType, $this->shippingType]))->setDefaults(new ArrayCollection([$this->billingType, $this->shippingType]));
     $accountAddressExpected = new AccountAddress();
     $accountAddressExpected->setPrimary(true)->addType($this->billingType)->addType($this->shippingType)->removeType($this->billingType)->removeType($this->shippingType)->addType($this->billingType)->addType($this->shippingType)->setDefaults(new ArrayCollection([$this->billingType, $this->shippingType]));
     $account = new Account();
     $account->addAddress($accountAddress1);
     $account->addAddress($accountAddress2);
     return ['FixAccountAddressesDefaultSubscriber check' => ['options' => [], 'defaultData' => $accountAddress1, 'viewData' => $accountAddress1, 'submittedData' => ['types' => [AddressType::TYPE_BILLING, AddressType::TYPE_SHIPPING], 'defaults' => ['default' => [AddressType::TYPE_BILLING, AddressType::TYPE_SHIPPING]], 'primary' => true], 'expectedData' => $accountAddressExpected, 'otherAddresses' => [$accountAddress2], 'updateOwner' => $account]];
 }