Spaces:
No application file
No application file
File size: 5,227 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
<?php
namespace MauticPlugin\MauticCrmBundle\Integration\Salesforce\CampaignMember;
use Mautic\PluginBundle\Entity\IntegrationEntityRepository;
use MauticPlugin\MauticCrmBundle\Integration\Salesforce\Exception\InvalidObjectException;
use MauticPlugin\MauticCrmBundle\Integration\Salesforce\Exception\NoObjectsToFetchException;
use MauticPlugin\MauticCrmBundle\Integration\Salesforce\Object\CampaignMember;
use MauticPlugin\MauticCrmBundle\Integration\Salesforce\Object\Contact;
use MauticPlugin\MauticCrmBundle\Integration\Salesforce\Object\Lead;
use MauticPlugin\MauticCrmBundle\Integration\Salesforce\QueryBuilder;
class Fetcher
{
private array $leads = [];
private array $knownLeadIds = [];
private array $unknownLeadIds = [];
private array $contacts = [];
private array $knownContactIds = [];
private array $unknownContactIds = [];
private array $mauticIds = [];
private array $knownCampaignMembers = [];
/**
* @param string|int $campaignId
*/
public function __construct(
private IntegrationEntityRepository $repo,
private Organizer $organizer,
private $campaignId
) {
$this->fetchLeads();
$this->fetchContacts();
}
/**
* Return SF query to fetch the object information for a CampaignMember.
*
* @throws NoObjectsToFetchException
* @throws InvalidObjectException
*/
public function getQueryForUnknownObjects(array $fields, $object): string
{
return match ($object) {
Lead::OBJECT => QueryBuilder::getLeadQuery($fields, $this->unknownLeadIds),
Contact::OBJECT => QueryBuilder::getContactQuery($fields, $this->unknownContactIds),
default => throw new InvalidObjectException(),
};
}
/**
* Fetch the Mautic contact IDs that are not already tracked as SF campaign members.
*/
public function getUnknownCampaignMembers(): array
{
// First, find those already tracked as part of this campaign
$this->fetchCampaignMembers();
// Second, find newly created objects
$this->fetchNewlyCreated();
$mauticLeadIds = array_map(
fn ($entity) => $entity['internal_entity_id'],
$this->knownCampaignMembers
);
return array_values(array_diff($this->mauticIds, $mauticLeadIds));
}
/**
* Fetch SF leads already identified.
*/
private function fetchLeads(): void
{
if (!$campaignMembers = $this->organizer->getLeadIds()) {
return;
}
$this->leads = $this->repo->getIntegrationsEntityId(
'Salesforce',
Lead::OBJECT,
'lead',
null,
null,
null,
false,
0,
0,
$campaignMembers
);
foreach ($this->leads as $lead) {
$this->knownLeadIds[] = $lead['integration_entity_id'];
$this->mauticIds[] = $lead['internal_entity_id'];
}
$this->unknownLeadIds = array_values(array_diff($campaignMembers, $this->knownLeadIds));
}
/**
* Fetch SF contacts already identified.
*/
private function fetchContacts(): void
{
if (!$campaignMembers = $this->organizer->getContactIds()) {
return;
}
$this->contacts = $this->repo->getIntegrationsEntityId(
'Salesforce',
Contact::OBJECT,
'lead',
null,
null,
null,
false,
0,
0,
$campaignMembers
);
foreach ($this->contacts as $contact) {
$this->knownContactIds[] = $contact['integration_entity_id'];
$this->mauticIds[] = $contact['internal_entity_id'];
}
$this->unknownContactIds = array_values(array_diff($campaignMembers, $this->knownContactIds));
}
/**
* Fetch SF campaign members already identified.
*/
private function fetchCampaignMembers(): void
{
if (!$this->mauticIds) {
return;
}
$this->knownCampaignMembers = $this->repo->getIntegrationsEntityId(
'Salesforce',
CampaignMember::OBJECT,
'lead',
$this->mauticIds,
null,
null,
false,
0,
0,
$this->campaignId
);
}
/**
* Fetch a list of all identified objects for SF contacts and leads.
*/
private function fetchNewlyCreated(): void
{
if (!$allUnknownContacts = array_merge($this->unknownLeadIds, $this->unknownContactIds)) {
return;
}
$newlyCreated = $this->repo->getIntegrationsEntityId(
'Salesforce',
null,
'lead',
null,
null,
null,
false,
0,
0,
$allUnknownContacts
);
foreach ($newlyCreated as $contact) {
$this->knownContactIds[] = $contact['integration_entity_id'];
$this->mauticIds[] = $contact['internal_entity_id'];
}
}
}
|