Integration/TrustedFormIntegration.php
public function doEnhancement(Lead $lead)
    {
        $persist = false;
        if ($lead->getFieldValue(self::CERT_URL_FIELD) && !$lead->getFieldValue('trusted_form_created_at')) {
            $trustedFormClaim = $lead->getFieldValue(self::CERT_URL_FIELD);
            $parts            = parse_url($trustedFormClaim);
            if ('https' !== $parts['scheme'] || self::CERT_REAL_HOST !== $parts['host']) {
                $this->logger->warning('Not Processing Suspicious TrustedForm URL: '.$trustedFormClaim);

                return false;
            }

            $parameters = $this->getFingers($lead);
            if ($lead->getId()) {
                $parameters['reference'] = ''.$lead->getId();
                $identifier              = $lead->getId();
            } else {
                $identifier = $lead->getEmail();
            }

            /** @var ArrayCollection|array $utmData */
            $utmData = $lead->getUtmTags();
            // Get the UTM Tags as an array of entities.
            if ($utmData instanceof ArrayCollection) {
                $utmData = $utmData->toArray();
            }
            if (is_array($utmData) && !empty($utmData)) {
                // Get the last UTM Source.
                $utmSources = [];
                /** @var UtmTag $utmTag */
                foreach ($utmData as $utmTag) {
                    if (!empty(trim($utmTag->getUtmSource()))) {
                        $utmSources[$utmTag->getDateAdded()->getTimestamp()] = $utmTag->getUtmSource();
                    }
                }
                ksort($utmSources);
                $parameters['vendor'] = array_pop($utmSources);
            }

            $authKeys = $this->getKeys();
            $settings = [
                'authorize_session' => true,
                'content_type'      => 'application/json',
                'encode_parameters' => 'json',
                'headers'           => ['Accept: application/json'],
                'return_raw'        => true,
                'curl_options'      => [
                    CURLOPT_USERPWD        => "$authKeys[username]:$authKeys[password]",
                    CURLOPT_CONNECTTIMEOUT => 1,
                    CURLOPT_TIMEOUT        => 10,
                ],
            ];

            for ($try = 0; $try < 3; ++$try) {
                $response = $this->makeRequest($trustedFormClaim, $parameters, 'post', $settings);
                if (!$response || !isset($response->body)) {
                    $this->logger->error(
                        'TrustedForm: Failed to respond with lead '.$identifier.'. Body: '.(!empty($response->body) ? $response->body : 'null')
                    );
                } else {
                    $data = json_decode($response->body);
                    switch ($response->code) {
                        case 200:
                        case 201:

                            // Set new value for xx_trusted_form_cert_url from $data->xx_trusted_form_cert_url
                            if (
                                !empty($data->{self::CERT_URL_FIELD})
                                && $data->{self::CERT_URL_FIELD} !== $lead->getFieldValue(self::CERT_URL_FIELD)
                            ) {
                                $lead->addUpdatedField(self::CERT_URL_FIELD, $data->{self::CERT_URL_FIELD});
                                $persist = true;
                            }

                            // Set new value for trusted_form_created_at from created_at
                            if (
                                !empty($data->created_at)
                                && $data->created_at !== $lead->getFieldValue('trusted_form_created_at')
                            ) {
                                $lead->addUpdatedField('trusted_form_created_at', $data->created_at);
                                $persist = true;
                            }

                            // Set new value for trusted_form_expires_at from expires_at
                            if (
                                !empty($data->expires_at)
                                && $data->expires_at !== $lead->getFieldValue('trusted_form_expires_at')
                            ) {
                                $lead->addUpdatedField('trusted_form_expires_at', $data->expires_at);
                                $persist = true;
                            }

                            // Set new value for trusted_form_share_url from share_url
                            if (
                                !empty($data->share_url)
                                && $data->share_url !== $lead->getFieldValue('trusted_form_share_url')
                            ) {
                                $lead->addUpdatedField('trusted_form_share_url', $data->share_url);
                                $persist = true;
                            }
                            $this->logger->info(
                                'TrustedForm: Contact '.$identifier.' '.(!$persist ? 'NOT ' : '').'updated. '.(!empty($data->message) ? $data->message : '')
                            );

                            if (!empty($data->warnings)) {
                                foreach ($data->warnings as $warning) {
                                    $this->logger->error('TrustedForm warning with contact '.$identifier.' '.$warning);
                                }
                            }
                            break 2;

                        case 404:
                            $this->logger->error(
                                'TrustedForm: Invalid Certificate: '.(!empty($data->message) ? $data->message : '')
                            );
                            break 2;

                        case 401:
                        case 403:
                            $this->logger->error(
                                'TrustedForm: Authentication Failure: '.(!empty($data->message) ? $data->message : '')
                            );
                            break 2;

                        case 502:
                        case 503:
                            $this->logger->error('TrustedForm: Exceeded rate limit (try '.($try + 1).'/3).');
                            // 100ms delay before retrying.
                            usleep(100000);
                            break;

                        default:
                            $this->logger->error(
                                'TrustedForm: Unrecognized response code: '.(!empty($data->code) ? $data->code : '').' '.(!empty($data->message) ? $data->message : '')
                            );
                            break 2;
                    }
                }
            }
        }

        return $persist;
    }
public function doEnhancement(Lead $lead)
    {
        $persist = false;
        if ($lead->getFieldValue(self::CERT_URL_FIELD) && !$lead->getFieldValue('trusted_form_created_at')) {
            $trustedFormClaim = $lead->getFieldValue(self::CERT_URL_FIELD);
            $parts            = parse_url($trustedFormClaim);
            if ('https' !== $parts['scheme'] || self::CERT_REAL_HOST !== $parts['host']) {
                $this->logger->warning('Not Processing Suspicious TrustedForm URL: '.$trustedFormClaim);

                return false;
            }

            $parameters = $this->getFingers($lead);
            if ($lead->getId()) {
                $parameters['reference'] = ''.$lead->getId();
                $identifier              = $lead->getId();
            } else {
                $identifier = $lead->getEmail();
            }

            /** @var ArrayCollection|array $utmData */
            $utmData = $lead->getUtmTags();
            // Get the UTM Tags as an array of entities.
            if ($utmData instanceof ArrayCollection) {
                $utmData = $utmData->toArray();
            }
            if (is_array($utmData) && !empty($utmData)) {
                // Get the last UTM Source.
                $utmSources = [];
                /** @var UtmTag $utmTag */
                foreach ($utmData as $utmTag) {
                    if (!empty(trim($utmTag->getUtmSource()))) {
                        $utmSources[$utmTag->getDateAdded()->getTimestamp()] = $utmTag->getUtmSource();
                    }
                }
                ksort($utmSources);
                $parameters['vendor'] = array_pop($utmSources);
            }

            $authKeys = $this->getKeys();
            $settings = [
                'authorize_session' => true,
                'content_type'      => 'application/json',
                'encode_parameters' => 'json',
                'headers'           => ['Accept: application/json'],
                'return_raw'        => true,
                'curl_options'      => [
                    CURLOPT_USERPWD        => "$authKeys[username]:$authKeys[password]",
                    CURLOPT_CONNECTTIMEOUT => 1,
                    CURLOPT_TIMEOUT        => 10,
                ],
            ];

            for ($try = 0; $try < 3; ++$try) {
                $response = $this->makeRequest($trustedFormClaim, $parameters, 'post', $settings);
                if (!$response || !isset($response->body)) {
                    $this->logger->error(
                        'TrustedForm: Failed to respond with lead '.$identifier.'. Body: '.(!empty($response->body) ? $response->body : 'null')
                    );
                } else {
                    $data = json_decode($response->body);
                    switch ($response->code) {
                        case 200:
                        case 201:

                            // Set new value for xx_trusted_form_cert_url from $data->xx_trusted_form_cert_url
                            if (
                                !empty($data->{self::CERT_URL_FIELD})
                                && $data->{self::CERT_URL_FIELD} !== $lead->getFieldValue(self::CERT_URL_FIELD)
                            ) {
                                $lead->addUpdatedField(self::CERT_URL_FIELD, $data->{self::CERT_URL_FIELD});
                                $persist = true;
                            }

                            // Set new value for trusted_form_created_at from created_at
                            if (
                                !empty($data->created_at)
                                && $data->created_at !== $lead->getFieldValue('trusted_form_created_at')
                            ) {
                                $lead->addUpdatedField('trusted_form_created_at', $data->created_at);
                                $persist = true;
                            }

                            // Set new value for trusted_form_expires_at from expires_at
                            if (
                                !empty($data->expires_at)
                                && $data->expires_at !== $lead->getFieldValue('trusted_form_expires_at')
                            ) {
                                $lead->addUpdatedField('trusted_form_expires_at', $data->expires_at);
                                $persist = true;
                            }

                            // Set new value for trusted_form_share_url from share_url
                            if (
                                !empty($data->share_url)
                                && $data->share_url !== $lead->getFieldValue('trusted_form_share_url')
                            ) {
                                $lead->addUpdatedField('trusted_form_share_url', $data->share_url);
                                $persist = true;
                            }
                            $this->logger->info(
                                'TrustedForm: Contact '.$identifier.' '.(!$persist ? 'NOT ' : '').'updated. '.(!empty($data->message) ? $data->message : '')
                            );

                            if (!empty($data->warnings)) {
                                foreach ($data->warnings as $warning) {
                                    $this->logger->error('TrustedForm warning with contact '.$identifier.' '.$warning);
                                }
                            }
                            break 2;

                        case 404:
                            $this->logger->error(
                                'TrustedForm: Invalid Certificate: '.(!empty($data->message) ? $data->message : '')
                            );
                            break 2;

                        case 401:
                        case 403:
                            $this->logger->error(
                                'TrustedForm: Authentication Failure: '.(!empty($data->message) ? $data->message : '')
                            );
                            break 2;

                        case 502:
                        case 503:
                            $this->logger->error('TrustedForm: Exceeded rate limit (try '.($try + 1).'/3).');
                            // 100ms delay before retrying.
                            usleep(100000);
                            break;

                        default:
                            $this->logger->error(
                                'TrustedForm: Unrecognized response code: '.(!empty($data->code) ? $data->code : '').' '.(!empty($data->message) ? $data->message : '')
                            );
                            break 2;
                    }
                }
            }
        }

        return $persist;
    }
@param Lead $lead

@return bool