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

MatchQueryBuilder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 78.56%
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 47
ccs 22
cts 28
cp 0.7856
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C with() 0 37 16
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