creatorId($favourite->artwork_id); if ($creatorId) { $this->userStats->incrementFavoritesReceived($creatorId); } // §7.5 On-demand: recompute behavior similarity when artwork reaches threshold $this->maybeRecomputeBehavior($favourite->artwork_id); } public function deleted(ArtworkFavourite $favourite): void { $creatorId = $this->creatorId($favourite->artwork_id); if ($creatorId) { $this->userStats->decrementFavoritesReceived($creatorId); } } private function creatorId(int $artworkId): ?int { $id = DB::table('artworks') ->where('id', $artworkId) ->value('user_id'); return $id !== null ? (int) $id : null; } /** * Dispatch on-demand behavior recomputation when an artwork crosses a * favourites threshold (5, 10, 25, 50 …). */ private function maybeRecomputeBehavior(int $artworkId): void { $count = (int) DB::table('artwork_favourites') ->where('artwork_id', $artworkId) ->count(); $thresholds = [5, 10, 25, 50, 100]; if (in_array($count, $thresholds, true)) { RecComputeSimilarByBehaviorJob::dispatch($artworkId)->delay(now()->addSeconds(30)); RecComputeSimilarHybridJob::dispatch($artworkId)->delay(now()->addMinute()); } } }