Initial import of animal_management module
This commit is contained in:
87
services/AnimalStreamPublisherService.php
Normal file
87
services/AnimalStreamPublisherService.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\animal_management\services;
|
||||
|
||||
use humhub\modules\animal_management\models\Animal;
|
||||
use humhub\modules\animal_management\models\AnimalMedicalVisit;
|
||||
use humhub\modules\animal_management\models\AnimalProgressUpdate;
|
||||
use humhub\modules\animal_management\models\AnimalStreamEntry;
|
||||
use humhub\modules\space\models\Space;
|
||||
use Yii;
|
||||
|
||||
class AnimalStreamPublisherService
|
||||
{
|
||||
public static function publishMedicalVisit(Animal $animal, AnimalMedicalVisit $visit): void
|
||||
{
|
||||
if (!self::streamTableExists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$exists = AnimalStreamEntry::find()->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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user