Completed
Push — master ( bce3ff...b78e58 )
by Konstantinos
04:07
created

TeamController::assignLeaderAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

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