Files
fewo-stommer/app/Actions/ApplyChannelRules.php
T
2026-10-27 21:51:56 +01:00

73 lines
3.6 KiB
PHP

<?php
namespace App\Actions;
use App\Channels\ICal\ICalRuleEngine;
use App\Models\BlockedPeriod;
use App\Models\Channel;
use App\Models\Property;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
class ApplyChannelRules
{
public function attributes(Channel $channel, ?BlockedPeriod $block, array $properties): array
{
$result = app(ICalRuleEngine::class)->evaluate($channel, $properties);
$attributes = ['ical_interpretation' => $result, 'ical_event_type' => $result['event_type'],
'leading_priority' => $result['representation'] === 'leading_candidate' ? max(1, 10001 - $result['priority']) : 0];
$previousOrigin = $block?->ical_interpretation['origin_channel_id'] ?? null;
if ($block === null || ($block->occupation_kind === null && $block->reviewed_at === null && ($block->origin_channel_id === null || $block->origin_channel_id === $previousOrigin))) {
$attributes['origin_channel_id'] = $result['origin_channel_id'];
}
return $attributes;
}
public function store(BlockedPeriod $block, array $attributes): void
{
foreach ($attributes as $field => $value) {
if ($block->getAttribute($field) !== $value) {
$block->forceFill([$field => $value]);
}
}
if ($block->isDirty()) {
$block->saveQuietly();
}
}
/** @return array{classified: int, missing_raw: int} */
public function handle(Channel $channel): array
{
return DB::transaction(function () use ($channel): array {
$channel = Channel::query()->whereKey($channel->id)->lockForUpdate()->firstOrFail();
app(ICalRuleEngine::class)->validate($channel, $channel->ical_rules ?? []);
$channel->forceFill(['ical_active_rules' => $channel->ical_rules ?? []])->save();
Property::query()->whereKey($channel->property_id)->lockForUpdate()->firstOrFail();
$events = collect($channel->ical_analysis['events'] ?? [])->keyBy('uid');
$counts = ['classified' => 0, 'missing_raw' => 0];
foreach ($channel->blockedPeriods()->where('source', 'channel')->lockForUpdate()->get() as $block) {
$event = $events->get($block->external_uid);
if ($event && $block->ical_raw !== null && $channel->ical_analyzed_at !== null
&& $block->updated_at->greaterThan($channel->ical_analyzed_at) && $block->ical_raw !== $event['raw']) {
throw ValidationException::withMessages(['rules' => 'Der Importbestand hat sich seit der Vorschau geändert. Den Kalender erneut analysieren und die Vorschau prüfen.']);
}
$properties = $event['properties'] ?? $block->ical_properties;
if ($properties === null) {
$counts['missing_raw']++;
continue;
}
$attributes = $this->attributes($channel, $block, $properties);
if ($event && $event['starts_on'] !== null && ($block->starts_on->toDateString() !== $event['starts_on'] || $block->ends_on->toDateString() !== $event['ends_on'])) {
$attributes = [...$attributes, 'needs_review' => true, 'review_note' => 'Die Analyse meldet einen anderen Zeitraum. Die Klassifikation ändert keine Termine; Kalenderabgleich und Zuordnung prüfen.'];
}
$this->store($block, [...$attributes, 'ical_properties' => $properties, 'ical_raw' => $event['raw'] ?? $block->ical_raw]);
$counts['classified']++;
}
return $counts;
});
}
}