HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.naniguide.com/app/Services/Ads/AdLeadFormService.php
<?php

namespace App\Services\Ads;

use App\Model\AdLead;
use App\Model\AdLeadFormConfig;
use App\Model\LocalCustomer;

class AdLeadFormService
{
    public function __construct(private AdPlatformManager $manager) {}

    public function create(LocalCustomer $customer, array $data): AdLeadFormConfig
    {
        $config = new AdLeadFormConfig();
        $config->uid = uniqid();
        $config->customer_id = $customer->id;
        $config->platform = $data['platform'];
        $config->form_id = $data['form_id'];
        $config->mail_list_id = $data['mail_list_id'] ?? null;
        $config->field_mapping = $data['field_mapping'] ?? ['email' => 'email'];
        $config->status = AdLeadFormConfig::STATUS_ACTIVE;
        $config->save();

        AdActivityLogger::log(
            entity: $config,
            action: 'lead_form.created',
            status: 'success',
            title: 'Lead form connected: ' . $config->form_id,
            platform: $config->platform,
        );

        return $config;
    }

    public function syncLeads(AdLeadFormConfig $config): int
    {
        // Real lead fetching via the adapter chain arrives with R10.  Until
        // then, mock installs seed sample leads so the UI keeps working.
        // Session pattern flag is NOT checked here — this service has no
        // AdPlatform record to call session() against.  R10 wires that.
        $created = 0;
        if ($this->manager->isMockMode()) {
            for ($i = 0; $i < 3; $i++) {
                AdLead::create([
                    'ad_lead_form_config_id' => $config->id,
                    'platform_lead_id' => 'mock_lead_' . $config->id . '_' . time() . '_' . $i,
                    'data' => [
                        'email' => 'lead' . $i . '@example.com',
                        'name' => 'Lead ' . ($i + 1),
                    ],
                    'synced_at' => now(),
                ]);
                $created++;
            }
        }

        $config->leads_count = $config->leads()->count();
        $config->last_synced_at = now();
        $config->save();

        AdActivityLogger::log(
            entity: $config,
            action: 'lead_form.synced',
            status: 'success',
            title: 'Lead form synced: ' . $created . ' new leads',
            platform: $config->platform,
        );

        return $created;
    }

    public function delete(AdLeadFormConfig $config): void
    {
        $name = $config->form_id;
        $config->delete();

        AdActivityLogger::log(
            entity: new AdLeadFormConfig(['customer_id' => 0]),
            action: 'lead_form.deleted',
            status: 'info',
            title: 'Lead form deleted: ' . $name,
        );
    }
}