Viewing file: SyncHilltopData.php (3.96 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command; use App\Services\HilltopAdsService; use App\Models\Campaign;
class SyncHilltopData extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'hilltop:sync-data {--campaign= : Sync specific campaign ID (optional)} {--date-range=7d : Date range to sync (24h, 7d, 30d, 90d)} {--mock : Generate mock data instead of calling API} {--force : Force sync even if recently synced}';
/** * The console command description. * * @var string */ protected $description = 'Fetch and sync campaign performance data from HilltopAds';
protected $hilltopService;
public function __construct(HilltopAdsService $hilltopService) { parent::__construct(); $this->hilltopService = $hilltopService; }
/** * Execute the console command. */ public function handle() { $campaignId = $this->option('campaign'); $dateRange = $this->option('date-range'); $useMock = $this->option('mock'); $force = $this->option('force');
$this->info('Starting HilltopAds data sync...'); $this->info("Date Range: {$dateRange}"); if ($useMock) { $this->warn('Using MOCK data generation mode'); }
if ($campaignId) { // Sync specific campaign $this->syncCampaign($campaignId, $dateRange, $useMock); } else { // Sync all campaigns $this->syncAllCampaigns($dateRange, $useMock); }
$this->info('Sync completed successfully!'); return Command::SUCCESS; }
/** * Sync a specific campaign */ protected function syncCampaign($campaignId, $dateRange, $useMock) { $campaign = Campaign::find($campaignId);
if (!$campaign) { $this->error("Campaign ID {$campaignId} not found!"); return Command::FAILURE; }
$this->info("Syncing campaign: {$campaign->name} (ID: {$campaign->id})");
if ($useMock) { $this->hilltopService->generateMockData($campaign->id, $dateRange); $this->info("✓ Mock data generated for campaign: {$campaign->name}"); } else { $success = $this->hilltopService->syncCampaignData($campaign->id, $dateRange); if ($success) { $this->info("✓ Synced campaign: {$campaign->name}"); } else { $this->error("✗ Failed to sync campaign: {$campaign->name}"); } } }
/** * Sync all campaigns */ protected function syncAllCampaigns($dateRange, $useMock) { $campaigns = Campaign::all();
if ($campaigns->isEmpty()) { $this->warn('No campaigns found to sync.'); return; }
$this->info("Found {$campaigns->count()} campaigns to sync");
$bar = $this->output->createProgressBar($campaigns->count()); $bar->start();
$successCount = 0; $failCount = 0;
foreach ($campaigns as $campaign) { if ($useMock) { $this->hilltopService->generateMockData($campaign->id, $dateRange); $successCount++; } else { $success = $this->hilltopService->syncCampaignData($campaign->id, $dateRange); if ($success) { $successCount++; } else { $failCount++; } } $bar->advance(); }
$bar->finish(); $this->newLine(2);
$this->info("Sync Summary:"); $this->info("✓ Successful: {$successCount}"); if ($failCount > 0) { $this->error("✗ Failed: {$failCount}"); } } }
|