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/AbstractDemoSeeder.php
<?php

namespace Database\Seeders\Demo;

use Illuminate\Database\Seeder;

/**
 * Base class for every Demo/* sub-seeder.
 *
 * Provides:
 *   - $this->log()  → consistent "[demo] ..." stdout
 *   - $this->time() → wraps a callable, prints elapsed time
 *   - $this->faker() → reset-on-call deterministic faker
 *
 * Sub-seeders MUST override run().
 */
abstract class AbstractDemoSeeder extends Seeder
{
    /**
     * Each sub-seeder gets a unique seed offset so different stages produce
     * different data even though they all derive from DemoConfig::FAKER_SEED.
     * Override in subclasses (default = 0).
     */
    protected int $fakerOffset = 0;

    protected function log(string $msg): void
    {
        $cls = (new \ReflectionClass($this))->getShortName();
        echo "[demo:{$cls}] {$msg}\n";
    }

    protected function time(string $label, callable $fn)
    {
        $t = microtime(true);
        $result = $fn();
        $ms = (int) round((microtime(true) - $t) * 1000);
        $this->log("{$label} — {$ms}ms");
        return $result;
    }

    protected function faker(): \Faker\Generator
    {
        return DemoFaker::reset($this->fakerOffset);
    }

    /**
     * Assign a random template to an Email row, going through TemplateService
     * exactly like the campaign wizard (`CampaignController::templateChoose()`).
     *
     * Pool: `SystemEmailTemplate` whose underlying Template is categorized
     * (Base / Extended / WooCommerce) — i.e. canonical admin-curated designs
     * seeded by `TemplateSeeder`. This excludes per-customer "[Campaign: …]"
     * copies and uncategorized junk.
     *
     * TemplateService::for($email)->setTemplate($tmpl) handles copyAsPrivate,
     * customer_id stamping, and associating template_id on the Email.
     */
    protected static ?\Illuminate\Support\Collection $systemTemplatePool = null;

    protected function assignRandomTemplate(\App\Model\Email $email): void
    {
        if (self::$systemTemplatePool === null) {
            self::$systemTemplatePool = \App\Model\SystemEmailTemplate::query()
                ->with('template.categories')
                ->whereHas('template', function ($q) {
                    $q->whereNotNull('content')
                      ->where('content', '!=', '')
                      ->whereHas('categories');
                })
                ->get()
                ->filter(fn ($s) => $s->template !== null);
        }
        if (self::$systemTemplatePool->isEmpty()) return;

        $sys = self::$systemTemplatePool->random();
        $name = $email->campaign?->name ?? $email->automationEmail?->name ?? $sys->name;
        \App\Services\TemplateService::for($email)->setTemplate($sys->template, $name);
    }
}