where(ExternalProfile::ATTRIBUTE_NAME, $name) ->where(ExternalProfile::ATTRIBUTE_SITE, $profileSite->value) ->first(); if ($findProfile instanceof ExternalProfile) { throw_if($findProfile->isClaimed(), Exception::class, "The external profile '{$findProfile->getName()}' is already claimed."); DB::rollBack(); return $findProfile; } $action = static::getActionClass($profileSite, $profileParameters); $storeAction = new StoreAction(); /** @var ExternalProfile $profile */ $profile = $storeAction->store($builder, [ ExternalProfile::ATTRIBUTE_EXTERNAL_USER_ID => $action->getUserId(), ExternalProfile::ATTRIBUTE_NAME => $name, ExternalProfile::ATTRIBUTE_SITE => $profileSite->value, ExternalProfile::ATTRIBUTE_VISIBILITY => ExternalProfileVisibility::fromLocalizedName($visibilityLocalized)->value, ]); DB::commit(); return $profile; } catch (Exception $e) { Log::error($e->getMessage()); DB::rollBack(); throw $e; } } /** * Get the mapping for the entries class. * * * @throws RuntimeException */ public static function getActionClass(ExternalProfileSite $site, ExternalProfile|array $profile): BaseExternalEntryUnclaimedAction { return match ($site) { ExternalProfileSite::ANILIST => new AnilistExternalEntryUnclaimedAction($profile), default => throw new RuntimeException("External entry action not configured for site {$site->localize()}"), }; } }