Passed
Push — master ( b73665...44e90f )
by Peter
32s
created

MatchesQuery::forPlayerIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PtrTn\Battlerite\Query\Matches;
4
5
use DateTime;
6
use PtrTn\Battlerite\Exception\InvalidQueryException;
7
use PtrTn\Battlerite\Query\CriterionInterface;
8
use PtrTn\Battlerite\Query\QueryInterface;
9
10
/**
11
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
12
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
13
 */
14
class MatchesQuery implements QueryInterface
15
{
16
    /**
17
     * @var CriterionInterface[]
18
     */
19
    private $criteria = [];
20
21 8
    private function __construct()
22
    {
23 8
    }
24
25 8
    public static function create(): self
26
    {
27 8
        return new self();
28
    }
29
30 2
    public function withOffset(int $offset): self
31
    {
32 2
        $this->addCriterion(new OffsetCriterion($offset));
33 2
        return $this;
34
    }
35
36 3
    public function withLimit(int $limit): self
37
    {
38 3
        $this->addCriterion(new LimitCriterion($limit));
39 3
        return $this;
40
    }
41
42 1
    public function sortBy(string $sortBy): self
43
    {
44 1
        $this->addCriterion(new SortAscendingCriterion($sortBy));
45 1
        return $this;
46
    }
47
48 1
    public function sortDescBy(string $sortBy): self
49
    {
50 1
        $this->addCriterion(new SortDescendingCriterion($sortBy));
51 1
        return $this;
52
    }
53
54 3
    public function withStartDate(DateTime $startDate)
55
    {
56 3
        $this->addCriterion(new StartDateCriterion($startDate));
57 2
        return $this;
58
    }
59
60 3
    public function withEndDate(DateTime $endDate)
61
    {
62 3
        $this->addCriterion(new EndDateCriterion($endDate));
63 2
        return $this;
64
    }
65
66 2
    public function forPlayerIds(array $playerIds)
67
    {
68 2
        $this->addCriterion(new PlayerIdsCriterion($playerIds));
69 2
        return $this;
70
    }
71
72 1
    public function forTeamNames(array $teamNames)
73
    {
74 1
        $this->addCriterion(new TeamNamesCriterion($teamNames));
75 1
        return $this;
76
    }
77
78 1
    public function forGameModes(array $gameModes)
79
    {
80 1
        $this->addCriterion(new GameModesCriterion($gameModes));
81 1
        return $this;
82
    }
83
84 5
    public function toQueryString(): ?string
85
    {
86 5
        $query = [];
87 5
        foreach ($this->criteria as $criterion) {
88 4
            $query = array_merge($query, $criterion->toArray());
89
        }
90
91 5
        if (!empty($query)) {
92 4
            return http_build_query($query);
93
        }
94
95 1
        return null;
96
    }
97
98 7
    private function addCriterion(CriterionInterface $criterionToAdd): void
99
    {
100 7
        $criterionToAdd->checkCollisionWithCriteria($this->criteria);
101 7
        foreach ($this->criteria as $criterion) {
102 3
            if (get_class($criterion) === get_class($criterionToAdd)) {
103 3
                throw InvalidQueryException::sameCriteria($criterionToAdd);
104
            }
105
        }
106 7
        $this->criteria[] = $criterionToAdd;
107 7
        return;
108
    }
109
}
110