Completed
Push — master ( bce3ff...b78e58 )
by Konstantinos
04:07
created

MatchQueryBuilder::with()   C

Complexity

Conditions 16
Paths 15

Size

Total Lines 37
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 18.5194
Metric Value
dl 0
loc 37
ccs 22
cts 28
cp 0.7856
rs 5.0151
cc 16
eloc 30
nc 15
nop 2
crap 18.5194

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 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