PlayerController::listAction()   F
last analyzed

Complexity

Conditions 17
Paths 272

Size

Total Lines 80
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
dl 0
loc 80
ccs 0
cts 40
cp 0
rs 3.8247
c 0
b 0
f 0
cc 17
eloc 46
nc 272
nop 3
crap 306

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use BZIon\Form\Creator\PlayerAdminNotesFormCreator as FormCreator;
4
use Carbon\Carbon;
5
use Symfony\Component\Form\Form;
6
use Symfony\Component\HttpFoundation\Request;
7
8
class PlayerController extends JSONController
9
{
10
    private $creator;
11
12
    public function showAction(Player $player, Player $me, Request $request)
13
    {
14
        $formView = null;
15
16
        if ($me->hasPermission(Permission::VIEW_VISITOR_LOG) && !$this->isDemoMode()) {
17
            $this->creator = new FormCreator($player);
18
            $form = $this->creator->create()->handleRequest($request);
19
20
            if ($form->isValid()) {
21
                $form = $this->handleAdminNotesForm($form, $player, $me);
22
            }
23
24
            $formView = $form->createView();
25
        }
26
27
        $periods = [];
28
        $season = Season::getCurrentSeasonRange();
29
        $periodLength = round($season->getEndOfRange()->diffInDays($season->getStartOfRange()) / 30);
30
        $seasonStart = $season->getStartOfRange();
31
32
        for ($i = 0; $i < $periodLength; $i++) {
33
            $periods[] = $seasonStart->firstOfMonth()->copy();
34
            $periods[] = $seasonStart->day(15)->copy();
35
            $periods[] = $seasonStart->lastOfMonth()->copy();
36
37
            $seasonStart->addMonth();
38
        }
39
40
        $currentPeriod = new ArrayIterator($periods);
41
        $playerEloSeason = $player->getEloSeasonHistory();
42
        $seasonSummary = [];
43
44
        foreach ($playerEloSeason as $elo) {
45
            if ($elo['month'] > $currentPeriod->current()->month || $elo['day'] > $currentPeriod->current()->day) {
46
                $currentPeriod->next();
47
            }
48
49
            $seasonSummary[$currentPeriod->current()->format('M d')] = $elo['elo'];
50
        }
51
52
        $bans = Ban::getQueryBuilder()
53
            ->where('player')->is($player->getId())
54
            ->getModels($fast = true)
55
        ;
56
57
        return array(
58
            'bans' => $bans,
59
            'player'         => $player,
60
            'seasonSummary'  => $seasonSummary,
61
            'adminNotesForm' => $formView,
62
        );
63
    }
64
65
    public function editAction(Player $player, Player $me)
66
    {
67
        if (!$me->canEdit($player)) {
68
            throw new ForbiddenException("You are not allowed to edit other players");
69
        }
70
71
        $params = array(
72
            'me'   => $player,
73
            'self' => false,
74
        );
75
76
        return $this->forward('edit', $params, 'Profile');
77
    }
78
79
    public function listAction(Request $request, Player $me, Team $team = null)
80
    {
81
        $query = Player::getQueryBuilder();
82
83
        // Load all countries into the cache so they are ready for later
84
        Country::getQueryBuilder()->addToCache();
85
86
        if ($team) {
87
            $query->where('team')->is($team);
88
        } else {
89
            // Add all teams to the cache
90
            $this->getQueryBuilder('Team')
91
                ->where('members')->greaterThan(0)
92
                ->addToCache();
93
        }
94
95
        if ($request->query->has('exceptMe')) {
96
            $query->except($me);
97
        }
98
99
        $groupBy = $request->query->get('groupBy');
100
        $sortBy = $request->query->get('sortBy');
101
        $sortOrder = $request->query->get('sortOrder');
102
103
        $query
104
            ->active()
105
            ->withMatchActivity()
106
            ->sortBy('name')
107
        ;
108
109
        if (!$request->query->get('showAll')) {
110
            $query->having('activity')->greaterThan(0);
111
        }
112
113
        if ($sortBy || $sortOrder) {
114
            $sortBy = $sortBy ? $sortBy : 'callsign';
115
            $sortOrder = $sortOrder ? $sortOrder : 'ASC';
116
117
            if ($sortBy === 'activity') {
118
                $query->sortBy($sortBy);
119
            }
120
121
            if ($sortOrder == 'DESC') {
122
                $query->reverse();
123
            }
124
        }
125
126
        $players = $query->getModels($fast = true);
127
128
        if ($groupBy) {
129
            $grouped = [];
130
131
            /** @var Player $player */
132
            foreach ($players as $player) {
133
                $key = '';
134
135
                if ($groupBy == 'country') {
136
                    $key = $player->getCountry()->getName();
137
                } elseif ($groupBy == 'team') {
138
                    $key = $player->getTeam()->getEscapedName();
139
140
                    if ($key == '<em>None</em>') {
141
                        $key = ' ';
142
                    }
143
                } elseif ($groupBy == 'activity') {
144
                    $key = ($player->getMatchActivity() > 0.0) ? 'Active' : 'Inactive';
145
                }
146
147
                $grouped[$key][] = $player;
148
            }
149
150
            ksort($grouped);
151
            $players = $grouped;
152
        }
153
154
        return array(
155
            'grouped' => ($groupBy !== null),
156
            'players' => $players,
157
        );
158
    }
159
160
    /**
161
     * Handle the admin notes form
162
     * @param  Form   $form   The form
163
     * @param  Player $player The player in question
164
     * @param  Player $me     The currently logged in player
165
     * @return Form   The updated form
166
     */
167
    private function handleAdminNotesForm($form, $player, $me)
168
    {
169
        $notes = $form->get('notes')->getData();
170
        if ($form->get('save_and_sign')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not seem to exist on object<Symfony\Component\Form\Form>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
171
            $notes .= ' — ' . $me->getUsername() . ' on ' . TimeDate::now()->toRFC2822String();
172
        }
173
174
        $player->setAdminNotes($notes);
175
        $this->getFlashBag()->add('success', "The admin notes for {$player->getUsername()} have been updated");
176
177
        // Reset the form so that the user sees the updated admin notes
178
        return $this->creator->create();
179
    }
180
}
181