Files
fotospiel-app/app/Services/AiEditing/EventAiEditingPolicyService.php
Codex Agent 36bed12ff9
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
feat: implement AI styling foundation and billing scope rework
2026-02-06 20:01:58 +01:00

91 lines
2.7 KiB
PHP

<?php
namespace App\Services\AiEditing;
use App\Models\AiStyle;
use App\Models\Event;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
class EventAiEditingPolicyService
{
/**
* @return array{
* enabled: bool,
* allow_custom_prompt: bool,
* allowed_style_keys: array<int, string>,
* policy_message: ?string
* }
*/
public function resolve(Event $event): array
{
$settings = is_array($event->settings) ? $event->settings : [];
$aiSettings = Arr::get($settings, 'ai_editing', []);
$aiSettings = is_array($aiSettings) ? $aiSettings : [];
$enabled = array_key_exists('enabled', $aiSettings)
? (bool) $aiSettings['enabled']
: true;
$allowCustomPrompt = array_key_exists('allow_custom_prompt', $aiSettings)
? (bool) $aiSettings['allow_custom_prompt']
: true;
$allowedStyleKeys = collect($aiSettings['allowed_style_keys'] ?? [])
->filter(fn (mixed $value): bool => is_string($value) && trim($value) !== '')
->map(fn (string $value): string => trim($value))
->unique()
->values()
->all();
$policyMessage = trim((string) ($aiSettings['policy_message'] ?? ''));
return [
'enabled' => $enabled,
'allow_custom_prompt' => $allowCustomPrompt,
'allowed_style_keys' => $allowedStyleKeys,
'policy_message' => $policyMessage !== '' ? $policyMessage : null,
];
}
public function isEnabled(Event $event): bool
{
return (bool) $this->resolve($event)['enabled'];
}
public function isStyleAllowed(Event $event, ?AiStyle $style): bool
{
$policy = $this->resolve($event);
if ($style === null) {
return (bool) $policy['allow_custom_prompt'];
}
/** @var array<int, string> $allowedStyleKeys */
$allowedStyleKeys = $policy['allowed_style_keys'];
if ($allowedStyleKeys === []) {
return true;
}
return in_array($style->key, $allowedStyleKeys, true);
}
/**
* @param Collection<int, AiStyle> $styles
* @return Collection<int, AiStyle>
*/
public function filterStyles(Event $event, Collection $styles): Collection
{
$policy = $this->resolve($event);
/** @var array<int, string> $allowedStyleKeys */
$allowedStyleKeys = $policy['allowed_style_keys'];
if ($allowedStyleKeys === []) {
return $styles->values();
}
return $styles
->filter(fn (AiStyle $style): bool => in_array($style->key, $allowedStyleKeys, true))
->values();
}
}