Completed
Pull Request — master (#160)
by Vladimir
24:16 queued 21:15
created

SeasonController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 10
dl 0
loc 130
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B showAction() 0 76 1
A parseSeason() 0 21 3
C validSeason() 0 28 7
1
<?php
2
3
use Symfony\Component\HttpFoundation\Request;
4
5
class SeasonController extends HTMLController
6
{
7
    public function showAction($season, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
8
    {
9
        $term = $year = '';
10
        $this->parseSeason($season, $term, $year);
11
12
        // Because this query can't be created efficiently using our QueryBuilder, let's do things manually
13
        $db = Database::getInstance();
14
        $seasonQuery = sprintf('
15
            SELECT %s, e.elo_new AS elo FROM players p 
16
              INNER JOIN player_elo e ON e.user_id = p.id 
17
              INNER JOIN (
18
                SELECT
19
                  user_id, 
20
                  MAX(match_id) AS last_match 
21
                FROM
22
                  player_elo 
23
                WHERE
24
                  season_period = ? AND season_year = ?
25
                GROUP BY
26
                  user_id
27
              ) i ON i.user_id = p.id AND i.last_match = e.match_id
28
            WHERE p.status = \'active\'
29
            ORDER BY elo DESC, p.username ASC LIMIT 10;
30
        ', Player::getEagerColumns('p'));
31
        $results = $db->query($seasonQuery, [$term, $year]);
32
        $players = Player::createFromDatabaseResults($results);
33
34
        $seasonRange = Season::getCurrentSeasonRange($term);
35
        $matchQuery = Match::getQueryBuilder();
36
        $matchQuery
37
            ->active()
38
            ->where('time')->isAfter($seasonRange->getStartOfRange($year), true)
39
            ->where('time')->isBefore($seasonRange->getEndOfRange($year), true)
40
        ;
41
42
        $fmQuery   = clone $matchQuery;
43
        $offiQuery = clone $matchQuery;
44
45
        $fmCount   = $fmQuery->where('type')->equals(Match::FUN)->count();
46
        $offiCount = $offiQuery->where('type')->equals(Match::OFFICIAL)->count();
47
48
        Map::getQueryBuilder()->addToCache();
49
        $mapQuery = '
50
            SELECT
51
                map AS map_id,
52
                COUNT(*) AS match_count
53
            FROM
54
                matches
55
            WHERE
56
                timestamp >= ? AND timestamp <= ? AND map IS NOT NULL
57
            GROUP BY
58
                map
59
            HAVING
60
                match_count > 0
61
            ORDER BY
62
                match_count DESC
63
        ';
64
        $results = $db->query($mapQuery, [
65
            $seasonRange->getStartOfRange($year),
66
            $seasonRange->getEndOfRange($year),
67
        ]);
68
69
        $mapIDs = array_column($results, 'map_id');
70
        $maps = Map::arrayIdToModel($mapIDs);
71
        $mapCount = array_combine($mapIDs, $results);
72
73
        return [
74
            'season'  => ucfirst($term),
75
            'year'    => $year,
76
            'players' => $players,
77
            'fmCount' => $fmCount,
78
            'offiCount' => $offiCount,
79
            'maps'    => $maps,
80
            'mapCount' => $mapCount
81
        ];
82
    }
83
84
    private function parseSeason($string, &$term, &$year)
85
    {
86
        $string = strtolower($string);
87
        $currentSeason = ($string === 'current');
88
89
        if (!$currentSeason) {
90
            $seasonTerm = explode('-', $string);
91
92
            if ($this->validSeason($seasonTerm)) {
93
                $term = $seasonTerm[0];
94
                $year = (int)$seasonTerm[1];
95
96
                return;
97
            }
98
        }
99
100
        $term = Season::getCurrentSeason();
101
        $year = TimeDate::now()->year;
102
103
        return;
0 ignored issues
show
Coding Style introduced by
Empty return statement not required here
Loading history...
104
    }
105
106
    private function validSeason($seasonSplit)
107
    {
108
        if (empty($seasonSplit) || count($seasonSplit) != 2) {
109
            return false;
110
        }
111
112
        if (in_array($seasonSplit[0], [Season::WINTER, Season::SPRING, Season::SUMMER, Season::FALL])) {
113
            $currentYear = TimeDate::now()->year;
114
            $seasonYear = (int)$seasonSplit[1];
115
116
            // The season's in the future
117
            if ($seasonYear > $currentYear) {
118
                return false;
119
            }
120
121
            // If the year's the same, we need to make sure the season's not in the future; e.g. Fall 2017 shouldn't be
122
            // valid when it's only July 2017
123
            if ($seasonYear == $currentYear &&
124
                Season::toInt($seasonSplit[0]) > Season::toInt(Season::getCurrentSeason())
125
            ) {
126
                return false;
127
            }
128
129
            return true;
130
        }
131
132
        return false;
133
    }
134
}
135