Completed
Push — master ( 345229...b82efc )
by Peter
01:52
created

MatchesQuery::toQueryString()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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