Skip to content

Commit

Permalink
Use a LegacyLeadModel to work around circular dependency for merging …
Browse files Browse the repository at this point in the history
…leads
  • Loading branch information
alanhartless committed Apr 26, 2018
1 parent 6bad9a0 commit 3a7151a
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 131 deletions.
8 changes: 8 additions & 0 deletions app/bundles/LeadBundle/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,13 @@
'mautic.lead.repository.lead',
],
],
// Deprecated support for circular dependency
'mautic.lead.legacy_merger' => [
'class' => \Mautic\LeadBundle\Deduplicate\LegacyContactMerger::class,
'arguments' => [
'service_container',
],
],
],
'repositories' => [
'mautic.lead.repository.dnc' => [
Expand Down Expand Up @@ -809,6 +816,7 @@
'mautic.user.provider',
'mautic.tracker.contact',
'mautic.tracker.device',
'mautic.lead.legacy_merger',
],
],
'mautic.lead.model.field' => [
Expand Down
9 changes: 8 additions & 1 deletion app/bundles/LeadBundle/Controller/LeadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Mautic\CoreBundle\Helper\EmojiHelper;
use Mautic\CoreBundle\Model\IteratorExportDataModel;
use Mautic\LeadBundle\DataObject\LeadManipulator;
use Mautic\LeadBundle\Deduplicate\ContactMerger;
use Mautic\LeadBundle\Deduplicate\Exception\SameContactException;
use Mautic\LeadBundle\Entity\DoNotContact;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\LeadModel;
Expand Down Expand Up @@ -873,7 +875,12 @@ public function mergeAction($objectId)
}

//Both leads are good so now we merge them
$mainLead = $model->mergeLeads($mainLead, $secLead, false);
/** @var ContactMerger $contactMerger */
$contactMerger = $this->container->get('mautic.lead.merger');
try {
$mainLead = $contactMerger->merge($mainLead, $secLead);
} catch (SameContactException $exception) {
}
}
}

Expand Down
157 changes: 27 additions & 130 deletions app/bundles/LeadBundle/Model/LeadModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Mautic\CoreBundle\Helper\IpLookupHelper;
use Mautic\CoreBundle\Helper\PathsHelper;
use Mautic\CoreBundle\Model\FormModel;
use Mautic\EmailBundle\Entity\Stat;
use Mautic\EmailBundle\Entity\StatRepository;
use Mautic\EmailBundle\Helper\EmailValidator;
use Mautic\LeadBundle\DataObject\LeadManipulator;
Expand All @@ -41,15 +40,13 @@
use Mautic\LeadBundle\Entity\LeadEventLog;
use Mautic\LeadBundle\Entity\LeadField;
use Mautic\LeadBundle\Entity\LeadList;
use Mautic\LeadBundle\Entity\MergeRecord;
use Mautic\LeadBundle\Entity\OperatorListTrait;
use Mautic\LeadBundle\Entity\PointsChangeLog;
use Mautic\LeadBundle\Entity\StagesChangeLog;
use Mautic\LeadBundle\Entity\Tag;
use Mautic\LeadBundle\Entity\UtmTag;
use Mautic\LeadBundle\Event\CategoryChangeEvent;
use Mautic\LeadBundle\Event\LeadEvent;
use Mautic\LeadBundle\Event\LeadMergeEvent;
use Mautic\LeadBundle\Event\LeadTimelineEvent;
use Mautic\LeadBundle\Helper\ContactRequestHelper;
use Mautic\LeadBundle\Helper\IdentifyCompanyHelper;
Expand Down Expand Up @@ -176,6 +173,11 @@ class LeadModel extends FormModel
*/
private $deviceTracker;

/**
* @var LegacyLeadModel
*/
private $legacyLeadModel;

/**
* @var bool
*/
Expand Down Expand Up @@ -210,6 +212,7 @@ class LeadModel extends FormModel
* @param UserProvider $userProvider
* @param ContactTracker $contactTracker
* @param DeviceTracker $deviceTracker
* @param LegacyLeadModel $legacyLeadModel
*/
public function __construct(
RequestStack $requestStack,
Expand All @@ -227,24 +230,26 @@ public function __construct(
EmailValidator $emailValidator,
UserProvider $userProvider,
ContactTracker $contactTracker,
DeviceTracker $deviceTracker
DeviceTracker $deviceTracker,
LegacyLeadModel $legacyLeadModel
) {
$this->request = $requestStack->getCurrentRequest();
$this->cookieHelper = $cookieHelper;
$this->ipLookupHelper = $ipLookupHelper;
$this->pathsHelper = $pathsHelper;
$this->integrationHelper = $integrationHelper;
$this->leadFieldModel = $leadFieldModel;
$this->leadListModel = $leadListModel;
$this->companyModel = $companyModel;
$this->formFactory = $formFactory;
$this->categoryModel = $categoryModel;
$this->channelListHelper = $channelListHelper;
$this->coreParametersHelper = $coreParametersHelper;
$this->emailValidator = $emailValidator;
$this->userProvider = $userProvider;
$this->contactTracker = $contactTracker;
$this->deviceTracker = $deviceTracker;
$this->request = $requestStack->getCurrentRequest();
$this->cookieHelper = $cookieHelper;
$this->ipLookupHelper = $ipLookupHelper;
$this->pathsHelper = $pathsHelper;
$this->integrationHelper = $integrationHelper;
$this->leadFieldModel = $leadFieldModel;
$this->leadListModel = $leadListModel;
$this->companyModel = $companyModel;
$this->formFactory = $formFactory;
$this->categoryModel = $categoryModel;
$this->channelListHelper = $channelListHelper;
$this->coreParametersHelper = $coreParametersHelper;
$this->emailValidator = $emailValidator;
$this->userProvider = $userProvider;
$this->contactTracker = $contactTracker;
$this->deviceTracker = $deviceTracker;
$this->legacyLeadModel = $legacyLeadModel;
}

/**
Expand Down Expand Up @@ -2427,16 +2432,6 @@ private function processManipulator(Lead $lead)
}
}

/**
* @param Lead $trackedLead
* @param array $queryFields
*
* @return Lead
*/
private function getContactFromClickthrough(Lead $trackedLead, array &$queryFields)
{
}

/**
* @param IpAddress $ip
* @param bool $persist
Expand Down Expand Up @@ -2688,7 +2683,7 @@ public function setSystemCurrentLead(Lead $lead = null)
/**
* Merge two leads; if a conflict of data occurs, the newest lead will get precedence.
*
* @deprecated 2.13.0; to be removed in 3.0. Use \Mautic\LeadBundle\Model\MergeModel instead
* @deprecated 2.13.0; to be removed in 3.0. Use \Mautic\LeadBundle\Deduplicate\ContactMerger instead
*
* @param Lead $lead
* @param Lead $lead2
Expand All @@ -2698,104 +2693,6 @@ public function setSystemCurrentLead(Lead $lead = null)
*/
public function mergeLeads(Lead $lead, Lead $lead2, $autoMode = true)
{
$this->logger->debug('LEAD: Merging leads');

$leadId = $lead->getId();
$lead2Id = $lead2->getId();

//if they are the same lead, then just return one
if ($leadId === $lead2Id) {
$this->logger->debug('LEAD: Leads are the same');

return $lead;
}

if ($autoMode) {
//which lead is the oldest?
$mergeWith = ($lead->getDateAdded() < $lead2->getDateAdded()) ? $lead : $lead2;
$mergeFrom = ($mergeWith->getId() === $leadId) ? $lead2 : $lead;
} else {
$mergeWith = $lead2;
$mergeFrom = $lead;
}
$this->logger->debug('LEAD: Lead ID# '.$mergeFrom->getId().' will be merged into ID# '.$mergeWith->getId());

//dispatch pre merge event
$event = new LeadMergeEvent($mergeWith, $mergeFrom);
if ($this->dispatcher->hasListeners(LeadEvents::LEAD_PRE_MERGE)) {
$this->dispatcher->dispatch(LeadEvents::LEAD_PRE_MERGE, $event);
}

//merge IP addresses
$ipAddresses = $mergeFrom->getIpAddresses();
foreach ($ipAddresses as $ip) {
$mergeWith->addIpAddress($ip);

$this->logger->debug('LEAD: Associating with IP '.$ip->getIpAddress());
}

//merge fields
$mergeFromFields = $mergeFrom->getFields();
foreach ($mergeFromFields as $group => $groupFields) {
foreach ($groupFields as $alias => $details) {
if ('points' === $alias) {
// We have to ignore this as it's a special field and it will reset the points for the contact
continue;
}

//overwrite old lead's data with new lead's if new lead's is not empty
if (!empty($details['value'])) {
$mergeWith->addUpdatedField($alias, $details['value']);

$this->logger->debug('LEAD: Updated '.$alias.' = '.$details['value']);
}
}
}

//merge owner
$oldOwner = $mergeWith->getOwner();
$newOwner = $mergeFrom->getOwner();

if ($oldOwner === null && $newOwner !== null) {
$mergeWith->setOwner($newOwner);

$this->logger->debug('LEAD: New owner is '.$newOwner->getId());
}

// Sum points
$mergeFromPoints = $mergeFrom->getPoints();
$mergeWithPoints = $mergeWith->getPoints();
$mergeWith->adjustPoints($mergeFromPoints);
$this->logger->debug('LEAD: Adding '.$mergeFromPoints.' points from lead ID #'.$mergeFrom->getId().' to lead ID #'.$mergeWith->getId().' with '.$mergeWithPoints.' points');

//merge tags
$mergeFromTags = $mergeFrom->getTags();
$addTags = $mergeFromTags->getKeys();
$this->modifyTags($mergeWith, $addTags, null, false);

//save the updated lead
$this->saveEntity($mergeWith, false);

// Update merge records for the lead about to be deleted
$this->getMergeRecordRepository()->moveMergeRecord($mergeFrom->getId(), $mergeWith->getId());

// Create an entry this contact was merged
$mergeRecord = new MergeRecord();
$mergeRecord->setContact($mergeWith)
->setDateAdded()
->setName($mergeFrom->getPrimaryIdentifier())
->setMergedId($mergeFrom->getId());
$this->getMergeRecordRepository()->saveEntity($mergeRecord);

//post merge events
if ($this->dispatcher->hasListeners(LeadEvents::LEAD_POST_MERGE)) {
$this->dispatcher->dispatch(LeadEvents::LEAD_POST_MERGE, $event);
}

//delete the old
$this->deleteEntity($mergeFrom);

//return the merged lead
return $mergeWith;
return $this->legacyLeadModel->mergeLeads($lead, $lead2, $autoMode);
}
}
70 changes: 70 additions & 0 deletions app/bundles/LeadBundle/Model/LegacyLeadModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* @copyright 2018 Mautic Contributors. All rights reserved
* @author Mautic, Inc.
*
* @link https://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\LeadBundle\Model;

use Mautic\LeadBundle\Deduplicate\ContactMerger;
use Mautic\LeadBundle\Deduplicate\Exception\SameContactException;
use Mautic\LeadBundle\Entity\Lead;
use Symfony\Component\DependencyInjection\Container;

/**
* Class LegacyLeadModel.
*
* @deprecated Used temporarily to get around circular depdenency for LeadModel
*/
class LegacyLeadModel
{
/**
* @var Container
*/
private $container;

/**
* LegacyContactMerger constructor.
*
* @param Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}

/**
* @param Lead $lead
* @param Lead $lead2
* @param bool $autoMode
*
* @return Lead
*/
public function mergeLeads(Lead $lead, Lead $lead2, $autoMode = true)
{
$leadId = $lead->getId();

if ($autoMode) {
//which lead is the oldest?
$winner = ($lead->getDateAdded() < $lead2->getDateAdded()) ? $lead : $lead2;
$loser = ($winner->getId() === $leadId) ? $lead2 : $lead;
} else {
$winner = $lead2;
$loser = $lead;
}

try {
/** @var ContactMerger $contactMerger */
$contactMerger = $this->container->get('mautic.lead.merger');

return $contactMerger->merge($winner, $loser);
} catch (SameContactException $exception) {
return $lead;
}
}
}

0 comments on commit 3a7151a

Please sign in to comment.