Completed
Push — fm-support ( 3b5d1b...2f8546 )
by Konstantinos
12:56
created

MatchQueryBuilder   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 83.02%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 23
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 103
ccs 44
cts 53
cp 0.8302
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D with() 0 47 18
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/player played
19
     *
20
     * @param  Team|Player $participant The team/player which played the matches
21
     * @param  string      $result      The outcome of the matches (win, draw or loss)
22
     * @return self
23
     */
24 2
    public function with($participant, $result = null)
25
    {
26 2
        if (!$participant || !$participant->isValid()) {
27
            return $this;
28
        }
29
30 2
        if ($participant instanceof Team) {
31 2
            $team_a_query = "team_a = ?";
32 2
            $team_b_query = "team_b = ?";
33
        } elseif ($participant instanceof Player) {
34
            $team_a_query = "FIND_IN_SET(?, team_a_players)";
35
            $team_b_query = "FIND_IN_SET(?, team_b_players)";
36
        } else {
37
            throw new InvalidArgumentException("Invalid model provided");
38
        }
39
40
        switch ($result) {
41 2
            case "wins":
42 2
            case "win":
43 2
            case "victory":
44 2
            case "victories":
45 1
                $query = "($team_a_query AND team_a_points > team_b_points) OR ($team_b_query AND team_b_points > team_a_points)";
46 1
                break;
47 2
            case "loss":
48 2
            case "lose":
49 2
            case "losses":
50 2
            case "defeat":
51 2
            case "defeats":
52
                $query = "($team_a_query AND team_b_points > team_a_points) OR ($team_b_query AND team_a_points > team_b_points)";
53
                break;
54 2
            case "draw":
55 2
            case "draws":
56 2
            case "tie":
57 2
            case "ties":
58
                $query = "($team_a_query OR $team_b_query) AND team_a_points = team_b_points";
59
                break;
60
            default:
61 2
                $query = "$team_a_query OR $team_b_query";
62
        }
63
64 2
        $this->conditions[] = $query;
65 2
        $this->parameters[] = $participant->getId();
66 2
        $this->parameters[] = $participant->getId();
67 2
        $this->types       .= 'ii';
68
69 2
        return $this;
70
    }
71
72
    /**
73
     * Group results by day
74
     *
75
     * @return $this
76
     */
77 1
    public function groupByMonth()
78
    {
79 1
        $this->groupQuery .= "GROUP BY YEAR(timestamp), MONTH(timestamp)";
80
81 1
        return $this;
82
    }
83
84
    /**
85
     * Get a count for each month's matches
86
     *
87
     * @param Team $team The team in question
88
     * @return array
89
     */
90 1
    public function getSummary(Team $team)
91
    {
92 1
        $this->groupByMonth();
93
94 1
        $query = $this->createQuery("YEAR(timestamp) as y, MONTH(timestamp) as m, COUNT(*) as count");
95
96 1
        $matches = array();
97 1
        $results = Database::getInstance()->query($query, $this->types, $this->parameters);
98
99 1
        foreach ($results as $match) {
100 1
            $matches[$match['y'] . '-' . sprintf('%02d', $match['m'])] = $match['count'];
101
        }
102
103
        // Add entries for dates with 0 matches
104 1
        $timestamp = $team->getCreationDate()->setTimezone('UTC')->startOfMonth();
105 1
        while ($timestamp->lte(TimeDate::now())) {
106 1
            $key = $timestamp->format('Y-m');
107 1
            if (!isset($matches[$key])) {
108 1
                $matches[$key] = 0;
109
            }
110
111 1
            $timestamp->addMonth();
112
        }
113 1
        ksort($matches);
114
115 1
        return $matches;
116
    }
117
}
118