/6-join-team-action.php Secret
Created
December 24, 2021 14:48
6 - Join team action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Actions\Teams; | |
... | |
class JoinTeamAction | |
{ | |
public function run(User $user, Team $team) | |
{ | |
// Have the user join this team and restore their contributions | |
$userPhotosOnThisTeam = $user->photos()->whereTeamId($team->id); | |
$user->teams()->attach($team, [ | |
'total_photos' => $userPhotosOnThisTeam->count(), | |
'total_litter' => $userPhotosOnThisTeam->sum('total_litter') | |
]); | |
$this->setAsActiveTeamIfNull($user, $team); | |
$team->members++; | |
$team->save(); | |
} | |
protected function setAsActiveTeamIfNull(User $user, Team $team): void | |
{ | |
if (is_null($user->active_team)) { | |
$user->active_team = $team->id; | |
$user->save(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment