예제 #1
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);
         }
     }
 }
예제 #2
0
 /**
  * @param $randomTemplate
  * @param User $owner
  * @param Contact $contact
  * @param EmailOrigin $origin
  *
  * @return EmailUser
  */
 protected function addEmailUser($randomTemplate, $owner, $contact, $origin)
 {
     $emailUser = $this->emailEntityBuilder->emailUser($this->templates[$randomTemplate]['Subject'], $owner->getEmail(), $contact->getPrimaryEmail()->getEmail(), new \DateTime('now'), new \DateTime('now'), new \DateTime('now'));
     $this->setSecurityContext($owner);
     $emailUser->setFolder($origin->getFolder(FolderType::SENT));
     $emailUser->setOwner($owner);
     $emailUser->setOrganization($owner->getOrganization());
     $emailBody = $this->emailEntityBuilder->body("Hi,\n" . $this->templates[$randomTemplate]['Text'], false, true);
     $emailUser->getEmail()->setEmailBody($emailBody);
     $emailUser->getEmail()->setMessageId(sprintf('<id.%s@%s', uniqid(), '@bap.migration.generated>'));
     return $emailUser;
 }
예제 #3
0
 public function testGetPrimaryEmail()
 {
     $contact = new Contact();
     $this->assertNull($contact->getPrimaryEmail());
     $email = new ContactEmail('*****@*****.**');
     $contact->addEmail($email);
     $this->assertNull($contact->getPrimaryEmail());
     $contact->setPrimaryEmail($email);
     $this->assertSame($email, $contact->getPrimaryEmail());
     $email2 = new ContactEmail('*****@*****.**');
     $contact->addEmail($email2);
     $contact->setPrimaryEmail($email2);
     $this->assertSame($email2, $contact->getPrimaryEmail());
     $this->assertFalse($email->isPrimary());
 }