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/database/seeders/Demo/DemoFaker.php
<?php

namespace Database\Seeders\Demo;

use Faker\Factory as FakerFactory;
use Faker\Generator;

/**
 * Deterministic faker shared by all demo sub-seeders.
 *
 * Reset seed at the start of each sub-seeder run via reset() so that
 * regenerating the demo dataset is reproducible.
 */
class DemoFaker
{
    protected static ?Generator $faker = null;

    public static function get(): Generator
    {
        if (self::$faker === null) {
            if (!class_exists(FakerFactory::class)) {
                throw new \RuntimeException(
                    "fakerphp/faker is not installed. Run: composer install (faker is a dev dependency)"
                );
            }
            self::$faker = FakerFactory::create('en_US');
            self::$faker->seed(DemoConfig::FAKER_SEED);
        }
        return self::$faker;
    }

    /** Reset to a known seed (use at the start of each sub-seeder). */
    public static function reset(int $seedOffset = 0): Generator
    {
        $f = self::get();
        $f->seed(DemoConfig::FAKER_SEED + $seedOffset);
        return $f;
    }

    // ─── Convenience builders for realistic content ─────────────────────────

    /** Plausible email built from first/last + a varied domain pool. */
    public static function emailFor(string $first, string $last): string
    {
        $f = self::get();
        $domains = ['gmail.com', 'yahoo.com', 'outlook.com', 'hotmail.com',
                    'icloud.com', 'fastmail.com', 'protonmail.com',
                    'company.io', 'startup.co', 'bigco.com', 'agency.net'];
        $sep   = $f->randomElement(['.', '_', '']);
        $local = strtolower($first . $sep . $last) . $f->optional(0.4)->numberBetween(1, 99);
        $local = preg_replace('/[^a-z0-9._]/', '', $local);
        return $local . '@' . $f->randomElement($domains);
    }

    /** Bulk-generate N unique fake emails with realistic distribution. */
    public static function uniqueEmails(int $count, string $listSlug = ''): array
    {
        $f = self::get();
        $emails = [];
        $i = 0;
        while (count($emails) < $count) {
            $first = strtolower($f->firstName);
            $last  = strtolower($f->lastName);
            $email = self::emailFor($first, $last);
            // Force uniqueness with monotonic suffix when collision happens.
            if (isset($emails[$email])) {
                $email = preg_replace('/@/', '+'. $listSlug . $i .'@', $email, 1);
            }
            $emails[$email] = true;
            $i++;
        }
        return array_keys($emails);
    }

    /** Realistic campaign subject line. */
    public static function campaignSubject(): string
    {
        $f = self::get();
        $templates = [
            '🎉 {topic} — special edition for you',
            'New {topic}: what you need to know',
            'Don\'t miss our {topic} update',
            '{topic} | Weekly digest',
            'Last chance: {topic} ends soon',
            'Inside: {topic} highlights & more',
            'Your {topic} report is ready',
            '✨ {topic} — handpicked just for you',
        ];
        $topics = [
            'Spring Sale', 'Product Launch', 'Newsletter', 'Holiday Deals',
            'Welcome Series', 'Customer Stories', 'Black Friday', 'Year in Review',
            'Feature Announcement', 'Webinar Invite', 'Discount Coupon', 'Cart Reminder',
        ];
        return strtr($f->randomElement($templates), [
            '{topic}' => $f->randomElement($topics),
        ]);
    }

    /** Realistic mail list name. */
    public static function listName(int $index, string $companySlug = ''): string
    {
        $f = self::get();
        $kinds = [
            'Newsletter Subscribers', 'Product Updates', 'VIP Customers',
            'Event Attendees', 'Webinar Signups', 'Free Trial Users',
            'Beta Testers', 'Past Buyers', 'New Leads', 'Cold Outreach',
            'Holiday Campaign', 'Re-engagement Pool',
        ];
        return $kinds[$index % count($kinds)] . ($companySlug ? " ({$companySlug})" : '');
    }

    /** Random "from name" for a list/campaign. */
    public static function fromName(string $companyName): string
    {
        $f = self::get();
        $patterns = [
            $companyName,
            'The ' . $companyName . ' Team',
            $f->firstName . ' from ' . $companyName,
            $companyName . ' Newsletter',
        ];
        return $f->randomElement($patterns);
    }
}