Help required to understand code section from mautic docs for creating lead and preventing duplicate

[h]Can somebody help me understand this code ?[/h] It can be found on the doc page https://developer.mautic.org/#manipulating-leads In creating new leads section.

When I paste this code in the code editor it gives me error, $availableLeadFields is not defined, and to be true I can’t see where it is defined and what is its use, I understand it is being used to compare the unique keys in fields, but How ?



I’ll be great full if anyone can help me understand this code, it’ll help a lot, I am making automated plugins and cron commands to help import leads from Google Sheet to Mautic, but every time there are a lot of duplicate entries I want to use this code to prevent duplicates, any help is appreciated. Thank you in advance.

Code:
// Optionally check for identifier fields to determine if the lead is unique $uniqueLeadFields = $this->factory->getModel('lead.field')->getUniqueIdentiferFields(); $uniqueLeadFieldData = array();

// Check if unique identifier fields are included
$inList = array_intersect_key($leadFields, $availableLeadFields);
foreach ($inList as $k => $v) {
if (empty($query[$k])) {
unset($inList[$k]);
}

if (array_key_exists($k, $uniqueLeadFields)) {
    $uniqueLeadFieldData[$k] = $v;
}

}

// If there are unique identifier fields, check for existing leads based on lead data
if (count($inList) && count($uniqueLeadFieldData)) {
$existingLeads = $this->em->getRepository(‘MauticLeadBundle:Lead’)->getLeadsByUniqueFields(
$uniqueLeadFieldData,
$leadId // If a currently tracked lead, ignore this ID when searching for duplicates
);
if (!empty($existingLeads)) {
// Existing found so merge the two leads
$lead = $leadModel->mergeLeads($lead, $existingLeads[0]);
}

// Get the lead's currently associated IPs
$leadIpAddresses = $lead->getIpAddresses();

// If the IP is not already associated, do so (the addIpAddress will automatically handle ignoring
// the IP if it is set to be ignored in the Configuration)
if (!$leadIpAddresses->contains($ipAddress)) {
    $lead->addIpAddress($ipAddress);
}

}

[h]Can somebody help me understand this code ?[/h] It can be found on the doc page https://developer.mautic.org/#manipulating-leads In creating new leads section.
When I paste this code in the code editor it gives me error, $availableLeadFields is not defined, and to be true I can’t see where it is defined and what is its use, I understand it is being used to compare the unique keys in fields, but How ?

I’ll be great full if anyone can help me understand this code, it’ll help a lot, I am making automated plugins and cron commands to help import leads from Google Sheet to Mautic, but every time there are a lot of duplicate entries I want to use this code to prevent duplicates, any help is appreciated. Thank you in advance.

// Optionally check for identifier fields to determine if the lead is unique
$uniqueLeadFields    = $this->factory->getModel('lead.field')->getUniqueIdentiferFields();
$uniqueLeadFieldData = array();

// Check if unique identifier fields are included
$inList = array_intersect_key($leadFields, $availableLeadFields);
foreach ($inList as $k => $v) {
    if (empty($query[$k])) {
        unset($inList[$k]);
    }

    if (array_key_exists($k, $uniqueLeadFields)) {
        $uniqueLeadFieldData[$k] = $v;
    }
}

// If there are unique identifier fields, check for existing leads based on lead data
if (count($inList) && count($uniqueLeadFieldData)) {
    $existingLeads = $this->em->getRepository('MauticLeadBundle:Lead')->getLeadsByUniqueFields(
        $uniqueLeadFieldData,
        $leadId // If a currently tracked lead, ignore this ID when searching for duplicates
    );
    if (!empty($existingLeads)) {
        // Existing found so merge the two leads
        $lead = $leadModel->mergeLeads($lead, $existingLeads[0]);
    }

    // Get the lead's currently associated IPs
    $leadIpAddresses = $lead->getIpAddresses();

    // If the IP is not already associated, do so (the addIpAddress will automatically handle ignoring
    // the IP if it is set to be ignored in the Configuration)
    if (!$leadIpAddresses->contains($ipAddress)) {
        $lead->addIpAddress($ipAddress);
    }
}