Completed
Pull Request — master (#186)
by Vladimir
05:50 queued 02:55
created

InvitationController::inviteAction()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 35
Code Lines 20

Duplication

Lines 13
Ratio 37.14 %

Importance

Changes 0
Metric Value
dl 13
loc 35
rs 8.439
c 0
b 0
f 0
cc 5
eloc 20
nc 5
nop 3
1
<?php
2
3
use BZIon\Event\Events;
4
use BZIon\Event\TeamInviteEvent;
5
use BZIon\Event\TeamJoinEvent;
6
use BZIon\Form\Creator\InvitationFormCreator;
7
use Symfony\Component\HttpFoundation\RedirectResponse;
8
use Symfony\Component\HttpFoundation\Response;
9
10
class InvitationController extends CRUDController
11
{
12
    public function acceptAction(Player $me, Invitation $invitation)
13
    {
14
        if (!$me->isTeamless()) {
15
            throw new ForbiddenException("You can't join a new team until you leave your current one.");
16
        }
17
18
        if ($invitation->getInvitedPlayer()->getId() != $me->getId()) {
19
            throw new ForbiddenException("This invitation isn't for you!");
20
        }
21
22
        if ($invitation->getExpiration()->lt(TimeDate::now())) {
23
            throw new ForbiddenException("This invitation has expired");
24
        }
25
26
        if ($invitation->getTeam()->isDeleted()) {
27
            $invitation->setExpired();
28
29
            throw new ForbiddenException("This invitation is for a team which has been deleted.");
30
        }
31
32
        $inviter = $invitation->getSentBy()->getEscapedUsername();
33
        $team    = $invitation->getTeam();
34
35 View Code Duplication
        return $this->showConfirmationForm(function () use ($invitation, $team, $me) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
            $team->addMember($me->getId());
37
            $invitation->setStatus(Invitation::STATUS_ACCEPTED);
38
            $invitation->setExpired();
39
            Service::getDispatcher()->dispatch(Events::TEAM_JOIN, new TeamJoinEvent($team, $me));
40
41
            return new RedirectResponse($team->getUrl());
42
        },  "Are you sure you want to accept the invitation from $inviter to join {$team->getEscapedName()}?",
43
            "You are now a member of {$team->getName()}");
44
    }
45
46
    public function inviteAction(Player $me, Team $team, Player $player)
47
    {
48
        if (!$me->canEdit($team)) {
49
            throw new ForbiddenException("You are not allowed to invite a player to that team!");
50
        }
51
52
        $creator = new InvitationFormCreator($team, $me, $this);
53
        $form = $creator->create()->handleRequest(self::getRequest());
54
55
        if ($form->isSubmitted()) {
56
            $this->validate($form);
57
58 View Code Duplication
            if ($form->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
                $clickedButton = $form->getClickedButton()->getName();
60
61
                if ($clickedButton === 'submit') {
62
                    $invitation = $creator->enter($form);
63
64
                    self::getFlashBag()->add('success', sprintf('"%s" has been invited to "%s."', $invitation->getInvitedPlayer()->getName(), $team->getName()));
65
66
                    return $this->redirectTo($team);
67
                }
68
69
                return (new RedirectResponse($this->getPreviousURL()));
70
            }
71
        } else {
72
            $form->get('invited_player')->setData($player);
73
        }
74
75
        return [
76
            'form' => $form->createView(),
77
            'team' => $team,
78
            'player' => $player,
79
        ];
80
    }
81
}
82