f208fe2f65
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
74 lines
2.3 KiB
PHP
74 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Autopodbor;
|
|
|
|
use App\Models\AutopodborSource;
|
|
use App\Models\Project;
|
|
use App\Models\Tenant;
|
|
use App\Services\Project\ProjectService;
|
|
|
|
final class AutopodborProjectCreator
|
|
{
|
|
public function __construct(private ProjectService $projects) {}
|
|
|
|
/**
|
|
* @param int[] $sourceIds
|
|
* @param array{regions:int[],daily_limit_target:int,delivery_days_mask:int} $common
|
|
* @return Project[]
|
|
*/
|
|
public function createFromSources(int $tenantId, array $sourceIds, array $common, bool $launch): array
|
|
{
|
|
$tenant = Tenant::findOrFail($tenantId);
|
|
$sources = AutopodborSource::where('tenant_id', $tenantId)
|
|
->whereIn('id', $sourceIds)->with('competitor')->get();
|
|
|
|
$created = [];
|
|
foreach ($sources as $src) {
|
|
$name = $this->uniqueName($tenantId, $this->displayName($src));
|
|
$project = $this->projects->create($tenant, [
|
|
'name' => $name,
|
|
'signal_type' => $src->signal_type,
|
|
'signal_identifier' => $src->identifier,
|
|
'daily_limit_target' => $common['daily_limit_target'],
|
|
'regions' => $common['regions'],
|
|
'delivery_days_mask' => $common['delivery_days_mask'],
|
|
]);
|
|
if (! $launch) {
|
|
$project->update(['is_active' => false, 'paused_at' => now()]);
|
|
$project = $project->fresh();
|
|
}
|
|
$src->update(['created_project_id' => $project->id]);
|
|
$created[] = $project;
|
|
}
|
|
|
|
return $created;
|
|
}
|
|
|
|
private function displayName(AutopodborSource $s): string
|
|
{
|
|
$n = $s->competitor->name;
|
|
if ($s->signal_type === 'call' && $s->phone_kind === 'real') {
|
|
return $n.' ✓';
|
|
}
|
|
if ($s->signal_type === 'call' && $s->phone_kind === 'substitute') {
|
|
return $n.' 🎭';
|
|
}
|
|
|
|
return $n;
|
|
}
|
|
|
|
private function uniqueName(int $tenantId, string $base): string
|
|
{
|
|
$name = $base;
|
|
$i = 1;
|
|
while (Project::where('tenant_id', $tenantId)->where('name', $name)->exists()) {
|
|
$i++;
|
|
$name = $base.' '.$i;
|
|
}
|
|
|
|
return $name;
|
|
}
|
|
}
|