Completed
Push — master ( e2e30a...b18a5e )
by Konstantinos
04:00
created

TeamController::kickAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0416
Metric Value
dl 0
loc 21
ccs 10
cts 12
cp 0.8333
rs 9.3142
cc 3
eloc 13
nc 3
nop 3
crap 3.0416
1
<?php
2
3
use BZIon\Event as Event;
4
use BZIon\Event\Events;
5
use Symfony\Component\Form\FormError;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
8
class TeamController extends CRUDController
9
{
10
    /**
11
     * The new leader if we're changing them
12
     * @var null|Player
13
     */
14
    private $newLeader = null;
15
16 1
    public function showAction(Team $team)
17
    {
18 1
        return array("team" => $team);
19
    }
20
21 1
    public function listAction()
22
    {
23 1
        return array("teams" => Team::getTeams());
24
    }
25
26
    public function createAction(Player $me)
27
    {
28
        return $this->create($me);
29
    }
30
31
    public function editAction(Player $me, Team $team)
32
    {
33
        $response = $this->edit($team, $me, "team");
34
35
        if ($this->newLeader) {
36
            // Redirect to a confirmation form if we are assigning a new leader
37
            $url = Service::getGenerator()->generate('team_assign_leader', array(
38
                'team'   => $team->getAlias(),
39
                'player' => $this->newLeader->getAlias()
40
            ));
41
42
            return new RedirectResponse($url);
43
        }
44
45
        return $response;
46
    }
47
48 1
    public function deleteAction(Player $me, Team $team)
49
    {
50 1
        $members = $team->getMembers();
51
52
        return $this->delete($team, $me, function () use ($team, $me, $members) {
53 1
            $event = new Event\TeamDeleteEvent($team, $me, $members);
54 1
            Service::getDispatcher()->dispatch(Events::TEAM_DELETE, $event);
55 1
        });
56
    }
57
58 View Code Duplication
    public function joinAction(Team $team, Player $me)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
60
        $this->requireLogin();
61
        if (!$me->isTeamless()) {
62
            throw new ForbiddenException("You are already a member of a team");
63
        } elseif ($team->getStatus() != 'open') {
64
            throw new ForbiddenException("This team is not accepting new members without an invitation");
65
        }
66
67
        return $this->showConfirmationForm(function () use ($team, $me) {
68
            $team->addMember($me->getId());
69
            Service::getDispatcher()->dispatch(Events::TEAM_JOIN,  new Event\TeamJoinEvent($team, $me));
70
71
            return new RedirectResponse($team->getUrl());
72
        },  "Are you sure you want to join {$team->getEscapedName()}?",
73
            "You are now a member of {$team->getName()}");
74
    }
75
76 1
    public function kickAction(Team $team, Player $player, Player $me)
77
    {
78 1
        $this->assertCanEdit($me, $team, "You are not allowed to kick a player off that team!");
79
80 1
        if ($team->getLeader()->isSameAs($player)) {
81
            throw new ForbiddenException("You can't kick the leader off their team.");
82
        }
83
84 1
        if (!$team->isMember($player->getId())) {
85
            throw new ForbiddenException("The specified player is not a member of that team.");
86
        }
87
88
        return $this->showConfirmationForm(function () use ($me, $team, $player) {
89 1
            $team->removeMember($player->getId());
90 1
            $event = new Event\TeamKickEvent($team, $player, $me);
91 1
            Service::getDispatcher()->dispatch(Events::TEAM_KICK, $event);
92
93 1
            return new RedirectResponse($team->getUrl());
94 1
        },  "Are you sure you want to kick {$player->getEscapedUsername()} from {$team->getEscapedName()}?",
95 1
            "Player {$player->getUsername()} has been kicked from {$team->getName()}", "Kick");
96
    }
97
98 1 View Code Duplication
    public function abandonAction(Team $team, Player $me)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
99
    {
100 1
        if (!$team->isMember($me->getId())) {
101
            throw new ForbiddenException("You are not a member of that team!");
102
        }
103
104 1
        if ($team->getLeader()->isSameAs($me)) {
105 1
            throw new ForbiddenException("You can't abandon the team you are leading.");
106
        }
107
108
        return $this->showConfirmationForm(function () use ($team, $me) {
109 1
            $team->removeMember($me->getId());
110 1
            Service::getDispatcher()->dispatch(Events::TEAM_ABANDON, new Event\TeamAbandonEvent($team, $me));
111
112 1
            return new RedirectResponse($team->getUrl());
113 1
        },  "Are you sure you want to abandon {$team->getEscapedName()}?",
114 1
            "You have left {$team->getName()}", "Abandon");
115
    }
116
117
    public function assignLeaderAction(Team $team, Player $me, Player $player)
118
    {
119
        $this->assertCanEdit($me, $team, "You are not allowed to change the leader of this team.");
120
121
        if (!$team->isMember($player->getId())) {
122
            throw new ForbiddenException("The specified player is not a member of {$team->getName()}");
123
        } elseif ($team->getLeader()->isSameAs($player)) {
124
            throw new ForbiddenException("{$player->getUsername()} is already the leader of {$team->getName()}");
125
        }
126
127
        return $this->showConfirmationForm(function () use ($player, $team) {
128
            $event = new Event\TeamLeaderChangeEvent($team, $player, $team->getLeader());
0 ignored issues
show
Bug introduced by
It seems like $team->getLeader() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
129
            $team->setLeader($player->getId());
130
            Service::getDispatcher()->dispatch(Events::TEAM_LEADER_CHANGE, $event);
131
132
            return new RedirectResponse($team->getUrl());
133
        }, "Are you sure you want to transfer the leadership of the team to <strong>{$player->getEscapedUsername()}</strong>?",
134
        "{$player->getUsername()} is now leading {$team->getName()}",
135
        "Appoint leadership");
136
    }
137
138
    /**
139
     * Show a confirmation form to confirm that the user wants to assign a new
140
     * leader to a team
141
     *
142
     * @param Player $leader The new leader
143
     */
144
    public function newLeader($leader)
145
    {
146
        $this->newLeader = $leader;
147
    }
148
149
    protected function validateNew($form)
150
    {
151
        $name = $form->get('name');
152
        $team = Team::getFromName($name->getData());
153
154
        // The name for the team that the user gave us already exists
155
        // TODO: This takes deleted teams into account, do we want that?
156
        if ($team->isValid()) {
157
            $name->addError(new FormError("A team called {$team->getName()} already exists"));
158
        }
159
    }
160
161
    protected function canCreate($player)
162
    {
163
        if ($player->getTeam()->isValid()) {
164
            throw new ForbiddenException("You need to abandon your current team before you can create a new one");
165
        }
166
167
        return parent::canCreate($player);
168
    }
169
170
    /**
171
     * Make sure that a player can edit a team
172
     *
173
     * Throws an exception if a player is not an admin or the leader of a team
174
     * @param  Player        $player  The player to test
175
     * @param  Team          $team    The team
176
     * @param  string        $message The error message to show
177
     * @throws HTTPException
178
     * @return void
179
     */
180 1
    private function assertCanEdit(Player $player, Team $team, $message = "You are not allowed to edit that team")
181
    {
182 1
        if (!$player->canEdit($team)) {
183
            throw new ForbiddenException($message);
184
        }
185 1
    }
186
}
187