Test Failed
Push — master ( c4dde2...b07990 )
by Peter
01:39
created

MatchesQuery::forTeamNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace PtrTn\Battlerite\Query;
4
5
use DateTime;
6
use Webmozart\Assert\Assert;
7
8
class MatchesQuery
9
{
10
    /**
11
     * @var int|null
12
     */
13
    private $offset;
14
15
    /**
16
     * @var int|null
17
     */
18
    private $limit;
19
20
    /**
21
     * @var string|null
22
     */
23
    private $sortBy;
24
25
    /**
26
     * @var DateTime|null
27
     */
28
    private $startDate;
29
30
    /**
31
     * @var DateTime|null
32
     */
33
    private $endDate;
34
35
    /**
36
     * @var string[]
37
     */
38
    private $playerIds;
39
40
    /**
41
     * @var string[]
42
     */
43
    private $teamNames;
44
45
    /**
46
     * @var string[]
47
     */
48
    private $gameModes;
49
50 7
    private function __construct()
51
    {
52 7
    }
53
54 7
    public static function create(): self
55
    {
56 7
        return new self();
57
    }
58
59 2
    public function withOffset(int $offset): self
60
    {
61 2
        $this->offset = $offset;
62 2
        return $this;
63
    }
64
65 3
    public function withLimit(int $limit): self
66
    {
67 3
        $this->limit = $limit;
68 3
        return $this;
69
    }
70
71
    public function sortBy(string $sortBy): self
72
    {
73
        $this->sortBy = $sortBy;
74
        return $this;
75
    }
76
77 2
    public function withStartDate(DateTime $startDate)
78
    {
79 2
        if (isset($this->endDate)) {
80 1
            Assert::greaterThan($this->endDate, $startDate, 'End date must be later than start date');
81
        }
82
83 1
        $this->startDate = $startDate;
84 1
        return $this;
85
    }
86
87 3
    public function withEndDate(DateTime $endDate)
88
    {
89 3
        if (isset($this->startDate)) {
90 1
            Assert::greaterThan($endDate, $this->startDate, 'End date must be later than start date');
91
        }
92
93 2
        $this->endDate = $endDate;
94 2
        return $this;
95
    }
96
97
    public function forPlayerIds(array $playerIds)
98
    {
99
        Assert::allString($playerIds, 'Specified player ids should be an array of strings');
100
        $this->playerIds = $playerIds;
101
        return $this;
102
    }
103
104
    public function forTeamNames(array $teamNames)
105
    {
106
        Assert::allString($teamNames, 'Specified team names should be an array of strings');
107
        $this->teamNames = $teamNames;
108
        return $this;
109
    }
110
111
    public function forGameModes(array $gameModes)
112
    {
113
        Assert::allString($gameModes, 'Specified game modes should be an array of strings');
114
        $this->gameModes = $gameModes;
115
        return $this;
116
    }
117
118 5
    public function toQueryString(): ?string
119
    {
120 5
        $query = [];
121 5
        if (isset($this->offset)) {
122 2
            $query['page[offset]'] = $this->offset;
123
        }
124 5
        if (isset($this->limit)) {
125 3
            $query['page[limit]'] = $this->limit;
126
        }
127 5
        if (isset($this->sortBy)) {
128
            $query['sort'] = $this->sortBy;
129
        }
130 5
        if (isset($this->startDate)) {
131
            $query['filter[createdAt-start]'] = $this->startDate->format('c');
132
        }
133 5
        if (isset($this->endDate)) {
134 1
            $query['filter[createdAt-end]'] = $this->endDate->format('c');
135
        }
136 5
        if (!empty($this->playerIds)) {
137
            $query['filter[playerIds]'] = implode($this->playerIds);
138
        }
139 5
        if (!empty($this->teamNames)) {
140
            $query['filter[teamNames]'] = implode($this->teamNames);
141
        }
142 5
        if (!empty($this->gameModes)) {
143
            $query['filter[gameMode]'] = implode($this->gameModes);
144
        }
145
146 5
        if (!empty($query)) {
147 4
            return http_build_query($query);
148
        }
149
150 1
        return null;
151
    }
152
}
153