where([ 'entry_type' => AnimalStreamEntry::TYPE_MEDICAL, 'medical_visit_id' => (int)$visit->id, ])->exists(); if ($exists) { return; } self::publishEntry($animal, AnimalStreamEntry::TYPE_MEDICAL, (int)$visit->id, null); } public static function publishProgressUpdate(Animal $animal, AnimalProgressUpdate $update): void { if (!self::streamTableExists()) { return; } $exists = AnimalStreamEntry::find()->where([ 'entry_type' => AnimalStreamEntry::TYPE_PROGRESS, 'progress_update_id' => (int)$update->id, ])->exists(); if ($exists) { return; } self::publishEntry($animal, AnimalStreamEntry::TYPE_PROGRESS, null, (int)$update->id); } private static function publishEntry(Animal $animal, string $entryType, ?int $medicalVisitId, ?int $progressUpdateId): void { try { $space = Space::findOne(['contentcontainer_id' => (int)$animal->contentcontainer_id]); if (!$space instanceof Space) { return; } $entry = new AnimalStreamEntry(); $entry->animal_id = (int)$animal->id; $entry->entry_type = $entryType; $entry->medical_visit_id = $medicalVisitId; $entry->progress_update_id = $progressUpdateId; $entry->content->container = $space; if (!$entry->save()) { Yii::warning([ 'message' => 'Could not save animal stream entry.', 'animal_id' => (int)$animal->id, 'entry_type' => $entryType, 'errors' => $entry->getErrors(), ], 'animal_management.stream_publish'); } } catch (\Throwable $e) { Yii::warning([ 'message' => 'Unexpected error while publishing animal stream entry.', 'animal_id' => (int)$animal->id, 'entry_type' => $entryType, 'exception' => $e->getMessage(), ], 'animal_management.stream_publish'); } } private static function streamTableExists(): bool { return Yii::$app->db->schema->getTableSchema('rescue_animal_stream_entry', true) !== null; } }