Completed
Push — master ( 8158f1...90d7fd )
by Konstantinos
11:17 queued 06:55
created

MatchQueryBuilder::with()   D

Complexity

Conditions 18
Paths 30

Size

Total Lines 46
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 24.009

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 46
ccs 25
cts 34
cp 0.7352
rs 4.9339
cc 18
eloc 37
nc 30
nop 2
crap 24.009

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
68 2
        return $this;
69
    }
70
71
    /**
72
     * Group results by day
73
     *
74
     * @return $this
75
     */
76 1
    public function groupByMonth()
77
    {
78 1
        $this->groupQuery .= "GROUP BY YEAR(timestamp), MONTH(timestamp)";
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * Get a count for each month's matches
85
     *
86
     * @param Team $team The team in question
87
     * @return array
88
     */
89 1
    public function getSummary(Team $team)
90
    {
91 1
        $this->groupByMonth();
92
93 1
        $query = $this->createQuery("YEAR(timestamp) as y, MONTH(timestamp) as m, COUNT(*) as count");
94
95 1
        $matches = array();
96 1
        $results = Database::getInstance()->query($query, $this->parameters);
97
98 1
        foreach ($results as $match) {
99 1
            $matches[$match['y'] . '-' . sprintf('%02d', $match['m'])] = $match['count'];
100
        }
101
102
        // Add entries for dates with 0 matches
103 1
        $timestamp = $team->getCreationDate()->setTimezone('UTC')->startOfMonth();
104 1
        while ($timestamp->lte(TimeDate::now())) {
105 1
            $key = $timestamp->format('Y-m');
106 1
            if (!isset($matches[$key])) {
107 1
                $matches[$key] = 0;
108
            }
109
110 1
            $timestamp->addMonth();
111
        }
112 1
        ksort($matches);
113
114 1
        return $matches;
115
    }
116
}
117