MatchQueryBuilder::with()   C
last analyzed

Complexity

Conditions 18
Paths 30

Size

Total Lines 51
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 24.009

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 25
cts 34
cp 0.7352
rs 5.5967
c 0
b 0
f 0
cc 18
eloc 38
nc 30
nop 2
crap 24.009

How to fix   Long Method    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
            $this->extras = 'INNER JOIN match_participation mp ON mp.match_id = matches.id';
35
            $team_a_query = 'mp.user_id = ? AND mp.team_loyalty = 0';
36
            $team_b_query = 'mp.user_id = ? AND mp.team_loyalty = 1';
37
        } else {
38
            throw new InvalidArgumentException("Invalid model provided");
39
        }
40
41 2
        switch ($result) {
42 2
            case "wins":
43 2
            case "win":
44 2
            case "victory":
45 1
            case "victories":
46 1
                $query = "($team_a_query AND team_a_points > team_b_points) OR ($team_b_query AND team_b_points > team_a_points)";
47 2
                break;
48 2
49 2
            case "loss":
50 2
            case "lose":
51 2
            case "losses":
52
            case "defeat":
53
            case "defeats":
54 2
                $query = "($team_a_query AND team_b_points > team_a_points) OR ($team_b_query AND team_a_points > team_b_points)";
55 2
                break;
56 2
57 2
            case "draw":
58
            case "draws":
59
            case "tie":
60
            case "ties":
61 2
                $query = "($team_a_query OR $team_b_query) AND team_a_points = team_b_points";
62
                break;
63
64 2
            default:
65 2
                $query = "$team_a_query OR $team_b_query";
66 2
67
        }
68 2
69
        $this->whereConditions[] = $query;
70
        $this->parameters[] = $participant->getId();
71
        $this->parameters[] = $participant->getId();
72
73
        return $this;
74
    }
75
76
    /**
77
     * Group results by day
78
     *
79
     * @return $this
80
     */
81
    public function groupByMonth()
82
    {
83
        $this->groupQuery .= "GROUP BY YEAR(timestamp), MONTH(timestamp)";
84
85
        return $this;
86
    }
87
88
    /**
89 1
     * Get a count for each month's matches
90
     *
91 1
     * @param TimeDate $timeDate The team in question
92
     *
93 1
     * @return array
94
     */
95 1
    public function getSummary(TimeDate $timeDate)
96 1
    {
97
        $this->groupQuery = 'GROUP BY match_date';
98 1
99 1
        $query = $this->createQuery("DATE_FORMAT(timestamp, '%Y-%m') AS match_date, COUNT(*) as match_count");
100
101
        $matches = [];
102 1
        $results = Database::getInstance()->query($query, $this->parameters);
103 1
104
        foreach ($results as $match) {
105
            $matches[$match['match_date']] = $match['match_count'];
106 1
        }
107 1
108
        $interval = new DateInterval('P1M');
109 1
        $dateRange = new DatePeriod($timeDate, $interval, TimeDate::now());
110 1
111
        /** @var DateTime $month */
112
        foreach($dateRange as $month) {
113
            $key = $month->format('Y-m');
114 1
115
            if (!isset($matches[$key])) {
116 1
                $matches[$key] = 0;
117
            }
118
        }
119
120
        ksort($matches);
121
122
        return $matches;
123
    }
124
}
125