Integration/CityStateFromPostalCodeIntegration.php
public function doEnhancement(Lead $lead)
    {
        $persist = false;

        $leadCity   = $lead->getCity();
        $leadState  = $lead->getState();
        $leadCounty = $lead->getFieldValue('county');

        // Get country in standard ISO3166 format.
        $leadCountry = $lead->getCountry();

        if (empty($leadCountry)) {
            /** @var mixed $ipDetails */
            $ipDetails   = $this->factory->getIpAddress()->getIpDetails();
            $leadCountry = isset($ipDetails['country']) ? strtoupper($ipDetails['country']) : 'US';
        } elseif (2 !== strlen($leadCountry)) {
            $leadCountry = $this->countryNameToISO3166($leadCountry);
        }

        // Get standardized zip without +4
        $leadZipCode = $lead->getZipcode();
        $dash        = strpos((string) $leadZipCode, '-');
        $leadZipCode = $dash ? substr((string) $leadZipCode, 0, $dash) : $leadZipCode;

        // Override User city, state if zip code given
        if (!(empty($leadCountry) || empty($leadZipCode))) {
            /** @var PluginEnhancerCityStatePostalCode $cityStatePostalCode */
            $cityStatePostalCode = $this->getIntegrationModel()->getRepository()->findOneBy(
                [
                    'postalCode' => $leadZipCode,
                    'country'    => $leadCountry,
                ]
            );

            if (null !== $cityStatePostalCode) {
                if (/*empty($leadCity) &&*/ !empty($cityStatePostalCode->getCity() && $leadCity !== $cityStatePostalCode->getCity())) {
                    $this->logger->debug('CityStateFromPostalCode: Found city for lead '.$lead->getId());
                    $lead->setCity($cityStatePostalCode->getCity());
                    $persist = true;
                }

                if (/*empty($leadState) &&*/ !empty($cityStatePostalCode->getStateProvince()) && $leadState !== $cityStatePostalCode->getStateProvince()) {
                    $this->logger->debug('CityStateFromPostalCode: Found state/province for lead '.$lead->getId());
                    $spLong  = $cityStatePostalCode->getStateProvince();
                    $spShort = in_array($spLong, self::$US_STATES)
                        ? array_search($spLong, self::$US_STATES)
                        : $spLong;
                    $lead->setState($spShort);
                    $persist = true;
                }

                if (/*empty($leadCounty) &&*/ !empty($cityStatePostalCode->getCounty()) && $leadCounty !== $cityStatePostalCode->getCounty()) {
                    $this->logger->debug('CityStateFromPostalCode: Found county for lead '.$lead->getId());
                    $lead->addUpdatedField('county', $cityStatePostalCode->getCounty(), $leadCounty);
                    $persist = true;
                }
            }
        } elseif (
            !(empty($leadCity) || empty($leadState) || empty($leadCountry)) &&
            empty($leadZipCode) &&
            empty($lead->getAddress1())
        ) {
            $stateProvince = array_key_exists($leadState, self::$US_STATES)
                ? self::$US_STATES[$leadState]
                : $leadState;
            /** @var PluginEnhancerCityStatePostalCode $cityStatePostalCode */
            $cityStatePostalCode = $this->getIntegrationModel()->getRepository()->findOneBy(
                [
                    'city'          => $leadCity,
                    'stateProvince' => $stateProvince,
                    'country'       => $leadCountry,
                ]
            );
            if (null !== $cityStatePostalCode) {
                $this->logger->debug('CityStateFromPostalCode: Found zipcode for lead '.$lead->getId());
                $lead->setZipcode($cityStatePostalCode->getPostalCode());
                $persist = true;
            } else {
                //handle non-exact city match
                /** @var QueryBuilder $qb */
                $qb = $this->em->getConnection()->createQueryBuilder();

                $qb->select(['city', 'MIN(postal_code) AS postal_code'])
                    ->from('plugin_enhancer_city_state_postal_code')
                    ->where(
                        'state_province = :state',
                        'country = :country'
                    )
                    ->groupBy('city')
                    ->setParameters([
                        'state'   => $stateProvince,
                        'country' => $leadCountry,
                    ]);

                $results = $qb->execute()->fetchAll();
                $picked  = [];
                foreach ($results as $result) {
                    if (empty($picked)) {
                        $picked['postal_code'] = $result['postal_code'];
                        $picked['distance']    = levenshtein($leadCity, $result['city']);
                    } else {
                        $distance = levenshtein($leadCity, $result['city']);
                        if ($distance < $picked['distance']) {
                            $picked['postal_code'] = $result['postal_code'];
                            $picked['distance']    = $distance;
                        }
                    }
                }
                if (isset($picked['distance']) && $picked['distance'] < self::MAX_LEV_DIST) {
                    $lead->setZipcode($picked['postal_code']);
                    $persist = true;
                }
            }
        }
        $this->em->clear('PluginEnhancerCityStatePostalCode');

        return $persist;
    }
public function doEnhancement(Lead $lead)
    {
        $persist = false;

        $leadCity   = $lead->getCity();
        $leadState  = $lead->getState();
        $leadCounty = $lead->getFieldValue('county');

        // Get country in standard ISO3166 format.
        $leadCountry = $lead->getCountry();

        if (empty($leadCountry)) {
            /** @var mixed $ipDetails */
            $ipDetails   = $this->factory->getIpAddress()->getIpDetails();
            $leadCountry = isset($ipDetails['country']) ? strtoupper($ipDetails['country']) : 'US';
        } elseif (2 !== strlen($leadCountry)) {
            $leadCountry = $this->countryNameToISO3166($leadCountry);
        }

        // Get standardized zip without +4
        $leadZipCode = $lead->getZipcode();
        $dash        = strpos((string) $leadZipCode, '-');
        $leadZipCode = $dash ? substr((string) $leadZipCode, 0, $dash) : $leadZipCode;

        // Override User city, state if zip code given
        if (!(empty($leadCountry) || empty($leadZipCode))) {
            /** @var PluginEnhancerCityStatePostalCode $cityStatePostalCode */
            $cityStatePostalCode = $this->getIntegrationModel()->getRepository()->findOneBy(
                [
                    'postalCode' => $leadZipCode,
                    'country'    => $leadCountry,
                ]
            );

            if (null !== $cityStatePostalCode) {
                if (/*empty($leadCity) &&*/ !empty($cityStatePostalCode->getCity() && $leadCity !== $cityStatePostalCode->getCity())) {
                    $this->logger->debug('CityStateFromPostalCode: Found city for lead '.$lead->getId());
                    $lead->setCity($cityStatePostalCode->getCity());
                    $persist = true;
                }

                if (/*empty($leadState) &&*/ !empty($cityStatePostalCode->getStateProvince()) && $leadState !== $cityStatePostalCode->getStateProvince()) {
                    $this->logger->debug('CityStateFromPostalCode: Found state/province for lead '.$lead->getId());
                    $spLong  = $cityStatePostalCode->getStateProvince();
                    $spShort = in_array($spLong, self::$US_STATES)
                        ? array_search($spLong, self::$US_STATES)
                        : $spLong;
                    $lead->setState($spShort);
                    $persist = true;
                }

                if (/*empty($leadCounty) &&*/ !empty($cityStatePostalCode->getCounty()) && $leadCounty !== $cityStatePostalCode->getCounty()) {
                    $this->logger->debug('CityStateFromPostalCode: Found county for lead '.$lead->getId());
                    $lead->addUpdatedField('county', $cityStatePostalCode->getCounty(), $leadCounty);
                    $persist = true;
                }
            }
        } elseif (
            !(empty($leadCity) || empty($leadState) || empty($leadCountry)) &&
            empty($leadZipCode) &&
            empty($lead->getAddress1())
        ) {
            $stateProvince = array_key_exists($leadState, self::$US_STATES)
                ? self::$US_STATES[$leadState]
                : $leadState;
            /** @var PluginEnhancerCityStatePostalCode $cityStatePostalCode */
            $cityStatePostalCode = $this->getIntegrationModel()->getRepository()->findOneBy(
                [
                    'city'          => $leadCity,
                    'stateProvince' => $stateProvince,
                    'country'       => $leadCountry,
                ]
            );
            if (null !== $cityStatePostalCode) {
                $this->logger->debug('CityStateFromPostalCode: Found zipcode for lead '.$lead->getId());
                $lead->setZipcode($cityStatePostalCode->getPostalCode());
                $persist = true;
            } else {
                //handle non-exact city match
                /** @var QueryBuilder $qb */
                $qb = $this->em->getConnection()->createQueryBuilder();

                $qb->select(['city', 'MIN(postal_code) AS postal_code'])
                    ->from('plugin_enhancer_city_state_postal_code')
                    ->where(
                        'state_province = :state',
                        'country = :country'
                    )
                    ->groupBy('city')
                    ->setParameters([
                        'state'   => $stateProvince,
                        'country' => $leadCountry,
                    ]);

                $results = $qb->execute()->fetchAll();
                $picked  = [];
                foreach ($results as $result) {
                    if (empty($picked)) {
                        $picked['postal_code'] = $result['postal_code'];
                        $picked['distance']    = levenshtein($leadCity, $result['city']);
                    } else {
                        $distance = levenshtein($leadCity, $result['city']);
                        if ($distance < $picked['distance']) {
                            $picked['postal_code'] = $result['postal_code'];
                            $picked['distance']    = $distance;
                        }
                    }
                }
                if (isset($picked['distance']) && $picked['distance'] < self::MAX_LEV_DIST) {
                    $lead->setZipcode($picked['postal_code']);
                    $persist = true;
                }
            }
        }
        $this->em->clear('PluginEnhancerCityStatePostalCode');

        return $persist;
    }
@param Lead $lead

@return bool