From b23b32dc4c19b08ade6c836de2f2a4a723aa7b60 Mon Sep 17 00:00:00 2001 From: miguel456 Date: Tue, 23 Aug 2022 03:38:56 +0100 Subject: [PATCH] refactor: reduce repetiton: add fluent conditional setter for users Upon setting a user, common validation will be performed, thus sparing the other methods of having to do this all the time. --- app/Helpers/Discord.php | 80 ++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 29 deletions(-) diff --git a/app/Helpers/Discord.php b/app/Helpers/Discord.php index e05c757..c2fa5d5 100644 --- a/app/Helpers/Discord.php +++ b/app/Helpers/Discord.php @@ -29,6 +29,7 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Http; +// Small wrapper for the necessary sections of the Discord API; A library is overkill here class Discord { @@ -39,6 +40,15 @@ class Discord protected string $workingGuild; + /** + * Current user. + * + * @var User The user all methods will affect. + */ + protected User $user; + + + public function __construct() { if (!is_null($this->workingGuild)) { $this->setWorkingGuild(config('services.discord.home_guild')); @@ -59,35 +69,49 @@ class Discord } /** - * Adds sepcified user to specified guild. Bot must be member of target guild, and account must be linked + * Sets the current user, upon validation * * @param User $user + * @return Discord + * @throws AccountNotLinkedException + */ + public function setUser(User $user): Discord + { + if ($user->hasDiscordConnection()) { + $this->user = $user; + return $this; + } + + throw new AccountNotLinkedException('Specified website user has not linked their Discord account yet.'); + } + + + + + /** + * Adds current working user to current working guild. Bot must be member of target guild, and account must be linked + * * @return object|bool A GuildMember object; false if member is already in guild - * @throws AccountNotLinkedException Thrown when user didn't link/create their account yet * @throws RequestException Any client and server errors * @see https://discord.com/developers/docs/resources/guild#guild-member-object */ - public function addGuildMember(User $user): object|bool + public function addGuildMember(): object|bool { - if ($user->hasDiscordConnection()) { - $params = [ - 'access_token' => $user->discord_token - ]; + $params = [ + 'access_token' => $this->user->discord_token + ]; - $member = Http::withBody(json_encode($params), 'application/json') - ->withHeaders([ - 'Authorization' => 'Bot ' . config('services.discord.token') - ])->put(config('services.discord.base_url') . "/guilds/{$this->workingGuild}/members/{$user->discord_user_id}") - ->throw(); + $member = Http::withBody(json_encode($params), 'application/json') + ->withHeaders([ + 'Authorization' => 'Bot ' . config('services.discord.token') + ])->put(config('services.discord.base_url') . "/guilds/{$this->workingGuild}/members/{$this->user->discord_user_id}") + ->throw(); - if ($member->successful() && $member->status() == 204) { - return false; - } else { - return (object) $member->json(); - } + if ($member->successful() && $member->status() == 204) { + return false; + } else { + return (object) $member->json(); } - - throw new AccountNotLinkedException('Specified Discord user has not linked their account yet.'); } @@ -95,21 +119,19 @@ class Discord * Bans a specified user from the guild. * May be called from the suspension service optionally by the banning user * - * @param User $user The user to ban from the Guild * @param string $reason The reason to supply Discord with * @return void Nothing on success * @throws RequestException + * @throws AccountNotLinkedException */ - public function addGuildBan(User $user, string $reason): void { - - if ($user->hasDiscordConnection()) { - $req = Http::withHeaders([ - 'Authorization' => 'Bot ' . config('services.discord.token'), - 'X-Audit-Log-Reason' => $reason - ])->put(config('services.discord.base_url') . "/guilds/{$this->workingGuild}/bans/{$user->discord_user_id}") - ->throw(); - } + public function addGuildBan(string $reason): void { + Http::withHeaders([ + 'Authorization' => 'Bot ' . config('services.discord.token'), + 'X-Audit-Log-Reason' => $reason + ])->put(config('services.discord.base_url') . "/guilds/{$this->workingGuild}/bans/{$this->user->discord_user_id}") + ->throw(); + throw new AccountNotLinkedException('Specified website user has not linked their Discord account yet.'); }