/**
  * Only one address must have one default address per type.
  *
  * @param CustomerAddress $address
  * @param CustomerAddress[] $allAddresses
  */
 protected function handleDefaultType(CustomerAddress $address, $allAddresses)
 {
     /** @var Collection|AddressType[] $addressDefaults */
     $addressDefaults = $address->getDefaults();
     foreach ($allAddresses as $otherAddresses) {
         if ($address === $otherAddresses) {
             continue;
         }
         $otherAddressDefaults = $otherAddresses->getDefaults();
         foreach ($addressDefaults as $addressDefaultType) {
             foreach ($otherAddressDefaults as $otherAddressDefault) {
                 if ($otherAddressDefault->getName() === $addressDefaultType->getName() && $otherAddressDefaults->contains($otherAddressDefault)) {
                     $otherAddressDefaults->removeElement($otherAddressDefault);
                 }
             }
         }
         $otherAddresses->setDefaults($otherAddressDefaults);
     }
 }
 public function testSetDefaults()
 {
     $types = new ArrayCollection([$this->billingType, $this->shippingType]);
     $this->assertCount(0, $this->address->getDefaults());
     $this->address->setTypes($types);
     $this->assertCount(0, $this->address->getDefaults());
     $this->assertInstanceOf('OroB2B\\Bundle\\CustomerBundle\\Entity\\CustomerAddress', $this->address->setDefaults([$this->billingType]));
     $this->assertCount(1, $this->address->getDefaults());
     $this->address->setDefaults([$this->billingType, $this->shippingType]);
     $this->assertCount(2, $this->address->getDefaults());
     $this->address->setTypes(new ArrayCollection([$this->billingType]));
     $this->assertCount(0, $this->address->getDefaults());
     $this->address->setDefaults([$this->shippingType]);
     $this->assertCount(0, $this->address->getDefaults());
 }
 /**
  * @param Customer $customer
  * @param CustomerAddress $address
  * @return array
  * @throws BadRequestHttpException
  */
 protected function update(Customer $customer, CustomerAddress $address)
 {
     $responseData = ['saved' => false, 'customer' => $customer];
     if ($this->getRequest()->getMethod() == 'GET' && !$address->getId()) {
         if (!$customer->getAddresses()->count()) {
             $address->setPrimary(true);
         }
     }
     if ($address->getOwner() && $address->getOwner()->getId() != $customer->getId()) {
         throw new BadRequestHttpException('Address must belong to customer');
     } elseif (!$address->getOwner()) {
         $customer->addAddress($address);
     }
     $form = $this->createForm(CustomerTypedAddressType::NAME, $address);
     $handler = new AddressHandler($form, $this->getRequest(), $this->getDoctrine()->getManagerForClass('OroB2BCustomerBundle:CustomerAddress'));
     if ($handler->process($address)) {
         $this->getDoctrine()->getManager()->flush();
         $responseData['entity'] = $address;
         $responseData['saved'] = true;
     }
     $responseData['form'] = $form->createView();
     return $responseData;
 }
 /**
  * @return array
  */
 public function submitWithFormSubscribersProvider()
 {
     $customerAddress1 = new CustomerAddress();
     $customerAddress1->setTypes(new ArrayCollection([$this->billingType, $this->shippingType]));
     $customerAddress2 = new CustomerAddress();
     $customerAddress2->setTypes(new ArrayCollection([$this->billingType, $this->shippingType]))->setDefaults(new ArrayCollection([$this->billingType, $this->shippingType]));
     $customerAddressExpected = new CustomerAddress();
     $customerAddressExpected->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]));
     $customer = new Customer();
     $customer->addAddress($customerAddress1);
     $customer->addAddress($customerAddress2);
     return ['FixCustomerAddressesDefaultSubscriber check' => ['options' => [], 'defaultData' => $customerAddress1, 'viewData' => $customerAddress1, 'submittedData' => ['types' => [AddressType::TYPE_BILLING, AddressType::TYPE_SHIPPING], 'defaults' => ['default' => [AddressType::TYPE_BILLING, AddressType::TYPE_SHIPPING]], 'primary' => true], 'expectedData' => $customerAddressExpected, 'otherAddresses' => [$customerAddress2], 'updateOwner' => $customer]];
 }
 /**
  * @param array $data
  * @return CustomerAddress
  */
 protected function createCustomerAddress(array $data)
 {
     /** @var Country $country */
     $country = $this->countryRepository->findOneBy(['iso2Code' => $data['country']]);
     if (!$country) {
         throw new \RuntimeException('Can\'t find country with ISO ' . $data['country']);
     }
     /** @var Region $region */
     $region = $this->regionRepository->findOneBy(['country' => $country, 'code' => $data['state']]);
     if (!$region) {
         throw new \RuntimeException(printf('Can\'t find region with country ISO %s and code %s', $data['country'], $data['state']));
     }
     $types = [];
     $typesFromData = explode(',', $data['types']);
     foreach ($typesFromData as $type) {
         $types[] = $this->addressTypeRepository->find($type);
     }
     $defaultTypes = [];
     $defaultTypesFromData = explode(',', $data['defaultTypes']);
     foreach ($defaultTypesFromData as $defaultType) {
         $defaultTypes[] = $this->addressTypeRepository->find($defaultType);
     }
     $address = new CustomerAddress();
     $address->setPrimary(true)->setLabel('Primary address')->setCountry($country)->setStreet($data['street'])->setCity($data['city'])->setRegion($region)->setPostalCode($data['zipCode'])->setTypes(new ArrayCollection($types))->setDefaults(new ArrayCollection($defaultTypes));
     return $address;
 }