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']) { if (! $this->hasEquivalentTimestampUpdate($channel, $block, $event)) { throw ValidationException::withMessages(['rules' => 'Der Importbestand hat sich seit der Vorschau geändert. Den Kalender erneut analysieren und die Vorschau prüfen.']); } $event = [...$event, 'properties' => $block->ical_properties, 'raw' => $block->ical_raw]; } $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; }); } private function hasEquivalentTimestampUpdate(Channel $channel, BlockedPeriod $block, array $event): bool { if ($block->ical_properties === null) { return false; } $withoutTimestamps = fn (string $raw): string => preg_replace('/^(?:DTSTAMP|LAST-MODIFIED)(?:;[^:\r\n]*)?:[^\r\n]*(?:\r\n|\n|$)/mi', '', $raw); if ($withoutTimestamps($block->ical_raw) !== $withoutTimestamps($event['raw'])) { return false; } $engine = app(ICalRuleEngine::class); return $engine->evaluate($channel, $block->ical_properties) === $engine->evaluate($channel, $event['properties']); } }