Completed
Push — fm-support ( 67f3c2...5f26cf )
by Vladimir
04:53
created

MatchQueryBuilder   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 86.96%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 21
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 93
ccs 40
cts 46
cp 0.8696
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C with() 0 37 16
A groupByMonth() 0 6 1
B getSummary() 0 27 4
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 1
    public function with($team, $result = null)
25
    {
26 1
        if (!$team || !$team->isValid()) {
27
            return $this;
28
        }
29
30
        switch ($result) {
31 1
            case "wins":
32 1
            case "win":
33 1
            case "victory":
34 1
            case "victories":
35 1
                $query = "(team_a = ? AND team_a_points > team_b_points) OR (team_b = ? AND team_b_points > team_a_points)";
36 1
                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 1
            case "ties":
48
                $query = "(team_a = ? OR team_b = ?) AND team_a_points = team_b_points";
49
                break;
50
            default:
51 1
                $query = "team_a = ? OR team_b = ?";
52
        }
53
54 1
        $this->conditions[] = $query;
55 1
        $this->parameters[] = $team->getId();
56 1
        $this->parameters[] = $team->getId();
57 1
        $this->types       .= 'ii';
58
59 1
        return $this;
60
    }
61
62
    /**
63
     * Group results by day
64
     *
65
     * @return $this
66
     */
67 1
    public function groupByMonth()
68
    {
69 1
        $this->groupQuery .= "GROUP BY YEAR(timestamp), MONTH(timestamp)";
70
71 1
        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 1
    public function getSummary(Team $team)
81
    {
82 1
        $this->groupByMonth();
83
84 1
        $query = $this->createQuery("YEAR(timestamp) as y, MONTH(timestamp) as m, COUNT(*) as count");
85
86 1
        $matches = array();
87 1
        $results = Database::getInstance()->query($query, $this->types, $this->parameters);
88
89 1
        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 1
        $timestamp = $team->getCreationDate()->setTimezone('UTC')->startOfMonth();
95 1
        while ($timestamp->lte(TimeDate::now())) {
96 1
            $key = $timestamp->format('Y-m');
97 1
            if (!isset($matches[$key])) {
98 1
                $matches[$key] = 0;
99
            }
100
101 1
            $timestamp->addMonth();
102
        }
103 1
        ksort($matches);
104
105 1
        return $matches;
106
    }
107
}
108