getUserId(); $profile = $this->firstForUserIdOrCreate($userId, $site, $action, $parameters); return $profile; } catch (Exception $e) { Log::error($e->getMessage()); throw $e; } } /** * Get the first record or create the profile for a userId and site. * * @param int $userId * @param ExternalProfileSite $site * @param BaseExternalEntryTokenAction $action * @param array $parameters * @return ExternalProfile */ protected function firstForUserIdOrCreate(int $userId, ExternalProfileSite $site, BaseExternalEntryTokenAction $action, array $parameters): ExternalProfile { $claimedProfile = ExternalProfile::query() ->where(ExternalProfile::ATTRIBUTE_EXTERNAL_USER_ID, $userId) ->where(ExternalProfile::ATTRIBUTE_SITE, $site->value) ->whereHas(ExternalProfile::RELATION_USER) ->first(); if ($claimedProfile instanceof ExternalProfile) { return $claimedProfile; } $unclaimedProfile = ExternalProfile::query() ->where(ExternalProfile::ATTRIBUTE_EXTERNAL_USER_ID, $userId) ->where(ExternalProfile::ATTRIBUTE_SITE, $site->value) ->whereDoesntHave(ExternalProfile::RELATION_USER) ->first(); if ($unclaimedProfile instanceof ExternalProfile) { $unclaimedProfile->update([ ExternalProfile::ATTRIBUTE_USER => Arr::get($parameters, ExternalProfile::ATTRIBUTE_USER), ExternalProfile::ATTRIBUTE_NAME => $action->getUsername(), ExternalProfile::ATTRIBUTE_VISIBILITY => ExternalProfileVisibility::PRIVATE->value, ]); return $unclaimedProfile; } $storeAction = new StoreAction(); /** @var ExternalProfile $profile */ $profile = $storeAction->store(ExternalProfile::query(), [ ExternalProfile::ATTRIBUTE_EXTERNAL_USER_ID => $userId, ExternalProfile::ATTRIBUTE_USER => Arr::get($parameters, ExternalProfile::ATTRIBUTE_USER), ExternalProfile::ATTRIBUTE_NAME => $action->getUsername(), ExternalProfile::ATTRIBUTE_SITE => $site->value, ExternalProfile::ATTRIBUTE_VISIBILITY => ExternalProfileVisibility::PRIVATE->value, ]); return $profile; } /** * Get the mapping for the entries token class. * * @param ExternalProfileSite $site * @param ExternalToken $token * @return BaseExternalEntryTokenAction * * @throws RuntimeException */ public static function getActionClass(ExternalProfileSite $site, ExternalToken $token): BaseExternalEntryTokenAction { return match ($site) { ExternalProfileSite::ANILIST => new AnilistExternalEntryTokenAction($token), ExternalProfileSite::MAL => new MalExternalEntryTokenAction($token), default => throw new RuntimeException("External entry token action not configured for site {$site->localize()}"), }; } }