Completed
Push — master ( 5e42b9...5a4659 )
by Konstantinos
03:52
created

MatchQueryBuilder::getSummary()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20
Metric Value
dl 0
loc 27
ccs 0
cts 0
cp 0
rs 8.5806
cc 4
eloc 15
nc 6
nop 1
crap 20
1
<?php
2
/**
3
 * This file contains a class to quickly generate database queries for matches
4
 *
5
 * @package    BZiON\Models\QueryBuilder
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
/**
10
 * This class can be used to search for matches with specific characteristics in
11
 * the database.
12
 *
13
 * @package    BZiON\Models\QueryBuilder
14
 */
15
class MatchQueryBuilder extends QueryBuilder
16
{
17
    /**
18
     * Only include matches where a specific team played
19
     *
20
     * @param  Team   $team   Team        The team which played the matches
21
     * @param  string $result string|null The outcome of the matches (win, draw or loss)
22
     * @return self
23
     */
24 2
    public function with($team, $result = null)
25
    {
26 2
        if (!$team || !$team->isValid()) {
27 1
            return $this;
28
        }
29
30
        switch ($result) {
31 2
            case "wins":
32 1
            case "win":
33 1
            case "victory":
34 1
            case "victories":
35
                $query = "(team_a = ? AND team_a_points > team_b_points) OR (team_b = ? AND team_b_points > team_a_points)";
36
                break;
37 1
            case "loss":
38 1
            case "lose":
39 1
            case "losses":
40 1
            case "defeat":
41 1
            case "defeats":
42
                $query = "(team_a = ? AND team_b_points > team_a_points) OR (team_b = ? AND team_a_points > team_b_points)";
43
                break;
44 1
            case "draw":
45 1
            case "draws":
46 1
            case "tie":
47 2
            case "ties":
48
                $query = "(team_a = ? OR team_b = ?) AND team_a_points = team_b_points";
49
                break;
50
            default:
51 2
                $query = "team_a = ? OR team_b = ?";
52
        }
53
54 2
        $this->conditions[] = $query;
55 2
        $this->parameters[] = $team->getId();
56 2
        $this->parameters[] = $team->getId();
57 2
        $this->types       .= 'ii';
58
59 2
        return $this;
60
    }
61
62
    /**
63
     * Group results by day
64
     *
65
     * @return $this
66
     */
67
    public function groupByMonth()
68
    {
69
        $this->groupQuery .= "GROUP BY YEAR(timestamp), MONTH(timestamp)";
70
71
        return $this;
72
    }
73
74
    /**
75
     * Get a count for each month's matches
76
     *
77
     * @param Team $team The team in question
78
     * @return array
79
     */
80
    public function getSummary(Team $team)
81
    {
82
        $this->groupByMonth();
83
84
        $query = $this->createQuery("YEAR(timestamp) as y, MONTH(timestamp) as m, COUNT(*) as count");
85
86
        $matches = array();
87
        $results = Database::getInstance()->query($query, $this->types, $this->parameters);
88
89
        foreach ($results as $match) {
90
            $matches[$match['y'] . '-' . sprintf('%02d', $match['m'])] = $match['count'];
91
        }
92
93
        // Add entries for dates with 0 matches
94
        $timestamp = $team->getCreationDate()->setTimezone('UTC')->startOfMonth();
95
        while ($timestamp->lte(TimeDate::now())) {
96
            $key = $timestamp->format('Y-m');
97
            if (!isset($matches[$key])) {
98
                $matches[$key] = 0;
99
            }
100
101
            $timestamp->addMonth();
102
        }
103
        ksort($matches);
104
105
        return $matches;
106
    }
107
}
108