animal; if ($animal === null) { return 0; } $fromSpace = $transfer->getFromSpace(); $toSpace = $transfer->getToSpace(); if (!$fromSpace instanceof Space || !$toSpace instanceof Space) { return 0; } $targetSpaces = static::targetSpacesForEvent($eventType, $fromSpace, $toSpace); $sentCount = 0; $seenRecipients = []; foreach ($targetSpaces as $space) { $recipients = static::privilegedUsersForSpace($space); foreach ($recipients as $recipient) { $recipientId = (int)$recipient->id; if ($originator instanceof User && $recipientId === (int)$originator->id) { continue; } if (isset($seenRecipients[$recipientId])) { continue; } $seenRecipients[$recipientId] = true; $notification = TransferStatusNotification::instance(); if ($originator instanceof User) { $notification->from($originator); } $notification->animalName = $animal->getDisplayName(); $notification->fromSpaceName = (string)$fromSpace->name; $notification->toSpaceName = (string)$toSpace->name; $notification->spaceGuid = (string)$space->guid; $notification->eventType = $eventType; $notification->details = $details; $notification->payload([ 'animalName' => $notification->animalName, 'fromSpaceName' => $notification->fromSpaceName, 'toSpaceName' => $notification->toSpaceName, 'spaceGuid' => $notification->spaceGuid, 'eventType' => $notification->eventType, 'details' => $notification->details, ]); $notification->send($recipient); $sentCount++; } } return $sentCount; } public static function privilegedUsersForSpace(Space $space): array { $recipients = []; foreach ($space->getPrivilegedGroupUsers() as $users) { foreach ($users as $user) { if ($user instanceof User && (int)$user->status === User::STATUS_ENABLED) { $recipients[(int)$user->id] = $user; } } } if (empty($recipients)) { $owner = $space->getOwnerUser()->one(); if ($owner instanceof User && (int)$owner->status === User::STATUS_ENABLED) { $recipients[(int)$owner->id] = $owner; } } return $recipients; } private static function targetSpacesForEvent(string $eventType, Space $fromSpace, Space $toSpace): array { $spaces = []; switch ($eventType) { case 'accepted': case 'declined': $spaces[] = $fromSpace; break; case 'cancelled': $spaces[] = $toSpace; break; case 'completed': $spaces[] = $fromSpace; break; default: $spaces[] = $fromSpace; $spaces[] = $toSpace; break; } $unique = []; foreach ($spaces as $space) { $key = (int)$space->contentcontainer_id; if (!isset($unique[$key])) { $unique[$key] = $space; } } return array_values($unique); } }