Completed
Push — feature/player-elo ( f9aa52...9accb0 )
by Vladimir
06:24
created

TeamController::abandonAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 18
loc 18
ccs 9
cts 10
cp 0.9
rs 9.4285
cc 3
eloc 11
nc 3
nop 2
crap 3.009
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
        $creationDate = $team->getCreationDate()->setTimezone('UTC')->startOfMonth();
19
20 1
        $matches = Match::getQueryBuilder()
21 1
            ->with($team)
22 1
            ->getSummary($creationDate);
23
24 1
        $wins = Match::getQueryBuilder()
25 1
            ->with($team, "win")
26 1
            ->getSummary($creationDate);
27
28
        return [
29 1
            'matches' => $matches,
30 1
            'wins'    => $wins,
31 1
            'team'    => $team
32
        ];
33
    }
34
35 1
    public function listAction()
36
    {
37 1
        Team::$cachedMatches = Match::getQueryBuilder()
38 1
            ->where('time')->isAfter(TimeDate::from('45 days ago'))
39 1
            ->active()
40 1
            ->getModels($fast = true);
41
42 1
        $teams = $this->getQueryBuilder()
43 1
            ->sortBy('elo')->reverse()
44 1
            ->getModels($fast = true);
45
46
        return array(
47 1
            "teams" => $teams
48
        );
49
    }
50
51
    public function createAction(Player $me)
52
    {
53
        return $this->create($me);
54
    }
55
56 View Code Duplication
    public function editAction(Player $me, Team $team)
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...
57
    {
58
        // TODO: Generating this response is unnecessary
59
        $response = $this->edit($team, $me, "team");
60
61
        if ($this->newLeader) {
62
            // Redirect to a confirmation form if we are assigning a new leader
63
            $url = Service::getGenerator()->generate('team_assign_leader', array(
64
                'team'   => $team->getAlias(),
65
                'player' => $this->newLeader->getAlias()
66
            ));
67
68
            return new RedirectResponse($url);
69
        }
70
71
        return $response;
72
    }
73
74 1
    public function deleteAction(Player $me, Team $team)
75
    {
76 1
        $members = $team->getMembers();
77
78
        return $this->delete($team, $me, function () use ($team, $me, $members) {
79 1
            $event = new Event\TeamDeleteEvent($team, $me, $members);
80 1
            Service::getDispatcher()->dispatch(Events::TEAM_DELETE, $event);
81 1
        });
82
    }
83
84 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...
85
    {
86
        $this->requireLogin();
87
        if (!$me->isTeamless()) {
88
            throw new ForbiddenException("You are already a member of a team");
89
        } elseif ($team->getStatus() != 'open') {
90
            throw new ForbiddenException("This team is not accepting new members without an invitation");
91
        }
92
93
        return $this->showConfirmationForm(function () use ($team, $me) {
94
            $team->addMember($me->getId());
95
            Service::getDispatcher()->dispatch(Events::TEAM_JOIN,  new Event\TeamJoinEvent($team, $me));
96
97
            return new RedirectResponse($team->getUrl());
98
        },  "Are you sure you want to join {$team->getEscapedName()}?",
99
            "You are now a member of {$team->getName()}");
100
    }
101
102 1
    public function kickAction(Team $team, Player $player, Player $me)
103
    {
104 1
        $this->assertCanEdit($me, $team, "You are not allowed to kick a player off that team!");
105
106 1
        if ($team->getLeader()->isSameAs($player)) {
107
            throw new ForbiddenException("You can't kick the leader off their team.");
108
        }
109
110 1
        if (!$team->isMember($player->getId())) {
111
            throw new ForbiddenException("The specified player is not a member of that team.");
112
        }
113
114
        return $this->showConfirmationForm(function () use ($me, $team, $player) {
115 1
            $team->removeMember($player->getId());
116 1
            $event = new Event\TeamKickEvent($team, $player, $me);
117 1
            Service::getDispatcher()->dispatch(Events::TEAM_KICK, $event);
118
119 1
            return new RedirectResponse($team->getUrl());
120 1
        },  "Are you sure you want to kick {$player->getEscapedUsername()} from {$team->getEscapedName()}?",
121 1
            "Player {$player->getUsername()} has been kicked from {$team->getName()}", "Kick");
122
    }
123
124 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...
125
    {
126 1
        if (!$team->isMember($me->getId())) {
127
            throw new ForbiddenException("You are not a member of that team!");
128
        }
129
130 1
        if ($team->getLeader()->isSameAs($me)) {
131 1
            throw new ForbiddenException("You can't abandon the team you are leading.");
132
        }
133
134
        return $this->showConfirmationForm(function () use ($team, $me) {
135 1
            $team->removeMember($me->getId());
136 1
            Service::getDispatcher()->dispatch(Events::TEAM_ABANDON, new Event\TeamAbandonEvent($team, $me));
137
138 1
            return new RedirectResponse($team->getUrl());
139 1
        },  "Are you sure you want to abandon {$team->getEscapedName()}?",
140 1
            "You have left {$team->getName()}", "Abandon");
141
    }
142
143
    public function assignLeaderAction(Team $team, Player $me, Player $player)
144
    {
145
        $this->assertCanEdit($me, $team, "You are not allowed to change the leader of this team.");
146
147
        if (!$team->isMember($player->getId())) {
148
            throw new ForbiddenException("The specified player is not a member of {$team->getName()}");
149
        } elseif ($team->getLeader()->isSameAs($player)) {
150
            throw new ForbiddenException("{$player->getUsername()} is already the leader of {$team->getName()}");
151
        }
152
153
        return $this->showConfirmationForm(function () use ($player, $team) {
154
            $event = new Event\TeamLeaderChangeEvent($team, $player, $team->getLeader());
155
            $team->setLeader($player->getId());
156
            Service::getDispatcher()->dispatch(Events::TEAM_LEADER_CHANGE, $event);
157
158
            return new RedirectResponse($team->getUrl());
159
        }, "Are you sure you want to transfer the leadership of the team to <strong>{$player->getEscapedUsername()}</strong>?",
160
        "{$player->getUsername()} is now leading {$team->getName()}",
161
        "Appoint leadership");
162
    }
163
164
    /**
165
     * Show a confirmation form to confirm that the user wants to assign a new
166
     * leader to a team
167
     *
168
     * @param Player $leader The new leader
169
     */
170
    public function newLeader($leader)
171
    {
172
        $this->newLeader = $leader;
173
    }
174
175
    protected function validateNew($form)
176
    {
177
        $name = $form->get('name');
178
        $team = Team::getFromName($name->getData());
179
180
        // The name for the team that the user gave us already exists
181
        // TODO: This takes deleted teams into account, do we want that?
182
        if ($team->isValid()) {
183
            $name->addError(new FormError("A team called {$team->getName()} already exists"));
184
        }
185
    }
186
187
    protected function canCreate($player)
188
    {
189
        if ($player->getTeam()->isValid()) {
190
            throw new ForbiddenException("You need to abandon your current team before you can create a new one");
191
        }
192
193
        return parent::canCreate($player);
194
    }
195
196
    /**
197
     * Make sure that a player can edit a team
198
     *
199
     * Throws an exception if a player is not an admin or the leader of a team
200
     * @param  Player        $player  The player to test
201
     * @param  Team          $team    The team
202
     * @param  string        $message The error message to show
203
     * @throws HTTPException
204
     * @return void
205
     */
206 1
    private function assertCanEdit(Player $player, Team $team, $message = "You are not allowed to edit that team")
207
    {
208 1
        if (!$player->canEdit($team)) {
209
            throw new ForbiddenException($message);
210
        }
211 1
    }
212
}
213