Exemple #1
0
 /**
  * @param string $phone
  * @param bool   $primary
  *
  * @return ContactPhone
  */
 protected function createContactPhone($phone, $primary = false)
 {
     $entity = new ContactPhone($phone);
     if ($primary) {
         $entity->setPrimary(true);
     }
     return $entity;
 }
 public function testGetPhoneNumbers()
 {
     $entity = new Contact();
     $this->assertSame([], $this->provider->getPhoneNumbers($entity));
     $phone1 = new ContactPhone('123-123');
     $entity->addPhone($phone1);
     $phone2 = new ContactPhone('456-456');
     $phone2->setPrimary(true);
     $entity->addPhone($phone2);
     $this->assertSame([['123-123', $entity], ['456-456', $entity]], $this->provider->getPhoneNumbers($entity));
 }
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     $contact = $this->getReference('Contact_' . LoadContactEntitiesData::FIRST_ENTITY_NAME);
     foreach ($this->contactEmailData as $contactEmailData) {
         $contactPhone = new ContactPhone();
         $contactPhone->setPrimary($contactEmailData['primary']);
         $contactPhone->setOwner($contact);
         $contactPhone->setPhone($contactEmailData['phone']);
         $this->setReference('ContactPhone_Several_' . $contactEmailData['phone'], $contactPhone);
         $manager->persist($contactPhone);
     }
     $contact2 = $this->getReference('Contact_' . LoadContactEntitiesData::SECOND_ENTITY_NAME);
     $contactPhone = new ContactPhone();
     $contactPhone->setPrimary($this->contactEmailData[0]['primary']);
     $contactPhone->setOwner($contact2);
     $contactPhone->setPhone($this->contactEmailData[0]['phone']);
     $this->setReference('ContactPhone_Single_' . $this->contactEmailData[0]['phone'], $contactPhone);
     $manager->persist($contactPhone);
     $manager->flush();
 }
Exemple #4
0
 /**
  * Create a Contact
  *
  * @param array $data
  * @param int $iteration
  * @return Contact
  */
 private function createContact(array $data, $iteration = 0)
 {
     $contact = new Contact();
     $contact->setFirstName($data['GivenName']);
     $lastName = $data['Surname'];
     if ($iteration) {
         $lastName .= '_' . $iteration;
     }
     $contact->setLastName($lastName);
     $contact->setNamePrefix($data['Title']);
     $phone = new ContactPhone($data['TelephoneNumber']);
     $phone->setPrimary(true);
     $contact->addPhone($phone);
     $email = new ContactEmail($data['EmailAddress']);
     $email->setPrimary(true);
     $contact->addEmail($email);
     $date = \DateTime::createFromFormat('m/d/Y', $data['Birthday']);
     $contact->setBirthday($date);
     /** @var ContactAddress $address */
     $address = new ContactAddress();
     $address->setCity($data['City']);
     $address->setStreet($data['StreetAddress']);
     $address->setPostalCode($data['ZipCode']);
     $address->setFirstName($data['GivenName']);
     $address->setLastName($data['Surname']);
     $address->setPrimary(true);
     $isoCode = $data['Country'];
     $country = array_filter($this->countries, function (Country $a) use($isoCode) {
         return $a->getIso2Code() == $isoCode;
     });
     $country = array_values($country);
     /** @var Country $country */
     $country = $country[0];
     $idRegion = $data['State'];
     /** @var Collection $regions */
     $regions = $country->getRegions();
     $region = $regions->filter(function (Region $a) use($idRegion) {
         return $a->getCode() == $idRegion;
     });
     $address->setCountry($country);
     if (!$region->isEmpty()) {
         $address->setRegion($region->first());
     }
     $contact->addAddress($address);
     return $contact;
 }
Exemple #5
0
 /**
  * Do merge of non-scalar fields such as emails or addresses
  *
  * @param Customer $remoteData
  * @param Customer $localData
  * @param Contact  $contact
  *
  * @SuppressWarnings(PHPMD)
  * TODO Should be fixed during CRM-1185
  */
 public function mergeObjects(Customer $remoteData, Customer $localData, Contact $contact)
 {
     // process emails
     $email = $contact->getPrimaryEmail();
     if ($email) {
         // if primary email exists try to merge
         $this->mergeScalars(['email'], $remoteData, $localData, $email);
     } elseif ($this->isRemotePrioritized()) {
         // if contact hasn't email and remote data has greater priority, then create it
         $email = new ContactEmail();
         $email->setPrimary(true);
         $email->setEmail($remoteData->getEmail());
         $contact->addEmail($email);
     }
     // process addresses
     $addresses = $contact->getAddresses();
     $isLocalTypesChanged = $this->isLocalAddressesTypesChanged($addresses, $localData);
     foreach ($addresses as $address) {
         // find in update local data if
         $localAddress = $this->getCustomerAddressByContactAddress($localData, $address);
         if (!$localAddress && $this->isRemotePrioritized()) {
             $contact->removeAddress($address);
         } elseif ($localAddress) {
             $remoteAddress = $this->getCorrespondentRemoteAddress($remoteData, $localAddress);
             if ($remoteAddress) {
                 // do update
                 $this->mergeScalars($this->addressScalarFields, $remoteAddress, $localAddress, $address);
                 if ($localAddress->getCountry()->getIso2Code() === $address->getCountry()->getIso2Code() || $this->isRemotePrioritized()) {
                     $address->setCountry($remoteAddress->getCountry());
                 }
                 if ($this->isRegionChanged($remoteAddress, $address) || $this->isRemotePrioritized()) {
                     $address->setRegion($remoteAddress->getRegion());
                     if ($address->getRegion()) {
                         $address->setRegionText(null);
                     }
                 }
                 if ($this->isRemotePrioritized() || !$isLocalTypesChanged) {
                     $address->setTypes($remoteAddress->getTypes());
                 }
                 $contactPhone = null;
                 if ($localAddress->getContactPhone()) {
                     $contactPhone = $this->getContactPhoneFromContact($contact, $localAddress->getContactPhone());
                 }
                 if ($contactPhone) {
                     $this->mergeScalars(['phone'], $remoteAddress, $localAddress, $contactPhone);
                 } elseif ($this->isRemotePrioritized() && $remoteAddress->getPhone() !== 'no phone') {
                     $contactPhone = new ContactPhone();
                     $contactPhone->setPhone($remoteAddress->getPhone());
                     $contactPhone->setPrimary(!$contact->getPrimaryPhone());
                     $contact->addPhone($contactPhone);
                     $localAddress->setContactPhone($contactPhone);
                 }
                 $this->prepareAddress($address);
                 if (!$address->getCountry()) {
                     $contact->removeAddress($address);
                 }
             } else {
                 $contact->removeAddress($address);
             }
         }
     }
     /** @var ArrayCollection|Address[] $newAddresses */
     $newAddresses = $this->getOrphanRemoteAddresses($remoteData, $localData);
     foreach ($newAddresses as $address) {
         /*
          * Will create new address if remote data has higher priority and means
          * that address removed from contact and remove should be cancelled.
          * Another case if it's newly created address, then process it anyway
          */
         if ($this->isRemotePrioritized() || !$address->getId()) {
             $contactAddress = new ContactAddress();
             $this->mergeScalars($this->addressScalarFields, $address, $contactAddress, $contactAddress);
             $contactAddress->setCountry($address->getCountry());
             $contactAddress->setRegion($address->getRegion());
             $contactAddress->setTypes($address->getTypes());
             $this->prepareAddress($contactAddress);
             if ($contactAddress->getCountry()) {
                 $contact->addAddress($contactAddress);
                 $address->setContactAddress($contactAddress);
             }
             if ($address->getContactPhone()) {
                 $address->getContactPhone()->setOwner($contact);
             }
         }
     }
     /** @var ContactAddress $toBePrimary */
     $toBePrimary = $contact->getAddresses()->first();
     if (!$contact->getPrimaryAddress() && $toBePrimary) {
         $toBePrimary->setPrimary(true);
     }
     // Set contact primary phone if none
     if (!$contact->getPrimaryPhone()) {
         if ($contact->getPhones()->count() > 0) {
             $contact->getPhones()->first()->setPrimary(true);
         }
     }
 }