Completed
Push — release/0.10.0 ( 3302b5...7ee28e )
by Vladimir
06:41 queued 04:04
created

TeamController::abandonAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Importance

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