whereRelation(ExternalToken::RELATION_PROFILE, ExternalProfile::ATTRIBUTE_USER, Auth::id()) ->whereRelation(ExternalToken::RELATION_PROFILE, ExternalProfile::ATTRIBUTE_SITE, $profileSite->value) ->first(); if (! $externalToken instanceof ExternalToken) { $externalToken = $this->getActionClass($profileSite)->store($parameters); } $profileAction = new StoreExternalProfileClaimedAction(); $profile = $profileAction->firstOrCreate($externalToken, $parameters); $profile->externaltoken()->save($externalToken); DB::commit(); return $profile; } catch (Exception $e) { Log::error($e->getMessage()); DB::rollBack(); throw $e; } } /** * Get the mapping for the token class. * * @throws RuntimeException */ protected function getActionClass(ExternalProfileSite $site): BaseExternalTokenAction { return match ($site) { ExternalProfileSite::ANILIST => new AnilistExternalTokenAction(), ExternalProfileSite::MAL => new MalExternalTokenAction(), default => throw new RuntimeException("External token action not configured for site {$site->localize()}"), }; } }