Completed
Push — feature/season-stats ( 6eff9a )
by Vladimir
03:25
created

SeasonController::validSeason()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 13
nc 5
nop 1
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
        return [
35
            'season'  => ucfirst($term),
36
            'year'    => $year,
37
            'players' => $players,
38
        ];
39
    }
40
41
    private function parseSeason($string, &$term, &$year)
42
    {
43
        $string = strtolower($string);
44
        $currentSeason = ($string === 'current');
45
46
        if (!$currentSeason) {
47
            $seasonTerm = explode('-', $string);
48
49
            if ($this->validSeason($seasonTerm)) {
50
                $term = $seasonTerm[0];
51
                $year = (int)$seasonTerm[1];
52
53
                return;
54
            }
55
        }
56
57
        $term = Season::getCurrentSeason();
58
        $year = TimeDate::now()->year;
59
60
        return;
0 ignored issues
show
Coding Style introduced by
Empty return statement not required here
Loading history...
61
    }
62
63
    private function validSeason($seasonSplit)
64
    {
65
        if (empty($seasonSplit) || count($seasonSplit) != 2) {
66
            return false;
67
        }
68
69
        if (in_array($seasonSplit[0], [Season::WINTER, Season::SPRING, Season::SUMMER, Season::FALL])) {
70
            $currentYear = TimeDate::now()->year;
71
            $seasonYear = (int)$seasonSplit[1];
72
73
            // The season's in the future
74
            if ($seasonYear > $currentYear) {
75
                return false;
76
            }
77
78
            // 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
79
            // valid when it's only July 2017
80
            if ($seasonYear == $currentYear &&
81
                Season::toInt($seasonSplit[0]) > Season::toInt(Season::getCurrentSeason())
82
            ) {
83
                return false;
84
            }
85
86
            return true;
87
        }
88
89
        return false;
90
    }
91
}
92