Skip to content

Commit

Permalink
Overhaul invitation database + model structure
Browse files Browse the repository at this point in the history
  • Loading branch information
allejo committed Feb 10, 2018
1 parent 9d6e1c6 commit e693095
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 56 deletions.
48 changes: 34 additions & 14 deletions controllers/InvitationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
use BZIon\Event\Events;
use BZIon\Event\TeamInviteEvent;
use BZIon\Event\TeamJoinEvent;
use BZIon\Form\Creator\InvitationFormCreator;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

class InvitationController extends CRUDController
{
public function acceptAction(Invitation $invitation, Player $me)
public function acceptAction(Player $me, Invitation $invitation)
{
if (!$me->isTeamless()) {
throw new ForbiddenException("You can't join a new team until you leave your current one.");
Expand All @@ -22,7 +24,7 @@ public function acceptAction(Invitation $invitation, Player $me)
}

if ($invitation->getTeam()->isDeleted()) {
$invitation->updateExpiration();
$invitation->setExpired();

throw new ForbiddenException("This invitation is for a team which has been deleted.");
}
Expand All @@ -32,30 +34,48 @@ public function acceptAction(Invitation $invitation, Player $me)

return $this->showConfirmationForm(function () use ($invitation, $team, $me) {
$team->addMember($me->getId());
$invitation->updateExpiration();
$invitation->setStatus(Invitation::STATUS_ACCEPTED);
$invitation->setExpired();
Service::getDispatcher()->dispatch(Events::TEAM_JOIN, new TeamJoinEvent($team, $me));

return new RedirectResponse($team->getUrl());
}, "Are you sure you want to accept the invitation from $inviter to join {$team->getEscapedName()}?",
"You are now a member of {$team->getName()}");
}

public function inviteAction(Team $team, Player $player, Player $me)
public function inviteAction(Player $me, Team $team, Player $player)
{
if (!$me->canEdit($team)) {
throw new ForbiddenException("You are not allowed to invite a player to that team!");
} elseif ($team->isMember($player->getId())) {
throw new ForbiddenException("The specified player is already a member of that team.");
} elseif (Invitation::hasOpenInvitation($player->getId(), $team->getId())) {
throw new ForbiddenException("This player has already been invited to join the team.");
}

return $this->showConfirmationForm(function () use ($team, $player, $me) {
$invite = Invitation::sendInvite($player->getId(), $team->getId(), $me->getId());
Service::getDispatcher()->dispatch(Events::TEAM_INVITE, new TeamInviteEvent($invite));
$creator = new InvitationFormCreator($team, $me, $this);
$form = $creator->create()->handleRequest(self::getRequest());

return new RedirectResponse($team->getUrl());
}, "Are you sure you want to invite {$player->getEscapedUsername()} to {$team->getEscapedName()}?",
"Player {$player->getUsername()} has been invited to {$team->getName()}");
if ($form->isSubmitted()) {
$this->validate($form);

if ($form->isValid()) {
$clickedButton = $form->getClickedButton()->getName();

if ($clickedButton === 'submit') {
$invitation = $creator->enter($form);

self::getFlashBag()->add('success', sprintf('"%s" has been invited to "%s."', $invitation->getInvitedPlayer()->getName(), $team->getName()));

return $this->redirectTo($team);
}

return (new RedirectResponse($this->getPreviousURL()));
}
} else {
$form->get('invited_player')->setData($player);
}

return [
'form' => $form->createView(),
'team' => $team,
'player' => $player,
];
}
}
48 changes: 48 additions & 0 deletions migrations/20180209070716_make_invitations_better.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use Phinx\Migration\AbstractMigration;

class MakeInvitationsBetter extends AbstractMigration
{
public function change()
{
$invitationsTable = $this->table('invitations');
$invitationsTable
->addColumn('sent', 'datetime', [
'after' => 'team',
'null' => true,
'default' => null,
'comment' => 'When the invitation was sent',
])
->addColumn('status', 'integer', [
'after' => 'text',
'null' => false,
'default' => 0,
'signed' => true,
'length' => 1,
'comment' => '0: pending; 1: accepted; 2: rejected',
])
->addColumn('is_deleted', 'boolean', [
'after' => 'status',
'null' => false,
'default' => false,
'comment' => 'Whether or not the invitation has been soft deleted',
])
->update()
;

$invitationsTable
->renameColumn('text', 'message')
->update()
;

$invitationsTable
->changeColumn('message', 'text', [
'null' => true,
'default' => null,
'comment' => 'The message sent when inviting a player to a team',
])
->update()
;
}
}
Loading

0 comments on commit e693095

Please sign in to comment.