Completed
Push — master ( be88d7...bcc77c )
by Peter
01:42
created

MatchesQuery::withStartDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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 6
    private function __construct()
51
    {
52 6
    }
53
54 6
    public static function create(): self
55
    {
56 6
        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 1
    public function sortBy(string $sortBy): self
72
    {
73 1
        $this->sortBy = $sortBy;
74 1
        return $this;
75
    }
76
77 3
    public function withStartDate(DateTime $startDate)
78
    {
79 3
        if (isset($this->endDate)) {
80 1
            Assert::greaterThan($this->endDate, $startDate, 'End date must be later than start date');
81
        }
82
83 2
        $this->startDate = $startDate;
84 2
        return $this;
85
    }
86
87 3
    public function withEndDate(DateTime $endDate)
88
    {
89 3
        if (isset($this->startDate)) {
90 2
            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 1
    public function forPlayerIds(array $playerIds)
98
    {
99 1
        Assert::allString($playerIds, 'Specified player ids should be an array of strings');
100 1
        $this->playerIds = $playerIds;
101 1
        return $this;
102
    }
103
104 1
    public function forTeamNames(array $teamNames)
105
    {
106 1
        Assert::allString($teamNames, 'Specified team names should be an array of strings');
107 1
        $this->teamNames = $teamNames;
108 1
        return $this;
109
    }
110
111 1
    public function forGameModes(array $gameModes)
112
    {
113 1
        Assert::allString($gameModes, 'Specified game modes should be an array of strings');
114 1
        $this->gameModes = $gameModes;
115 1
        return $this;
116
    }
117
118
    /**
119
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
120
     * @SuppressWarnings(PHPMD.NPathComplexity)
121
     */
122 4
    public function toQueryString(): ?string
123
    {
124 4
        $query = [];
125 4
        if (isset($this->offset)) {
126 2
            $query['page[offset]'] = $this->offset;
127
        }
128 4
        if (isset($this->limit)) {
129 3
            $query['page[limit]'] = $this->limit;
130
        }
131 4
        if (isset($this->sortBy)) {
132 1
            $query['sort'] = $this->sortBy;
133
        }
134 4
        if (isset($this->startDate)) {
135 1
            $query['filter[createdAt-start]'] = $this->startDate->format(DateTime::ISO8601);
136
        }
137 4
        if (isset($this->endDate)) {
138 1
            $query['filter[createdAt-end]'] = $this->endDate->format(DateTime::ISO8601);
139
        }
140 4
        if (!empty($this->playerIds)) {
141 1
            $query['filter[playerIds]'] = implode(',', $this->playerIds);
142
        }
143 4
        if (!empty($this->teamNames)) {
144 1
            $query['filter[teamNames]'] = implode(',', $this->teamNames);
145
        }
146 4
        if (!empty($this->gameModes)) {
147 1
            $query['filter[gameMode]'] = implode(',', $this->gameModes);
148
        }
149
150 4
        if (!empty($query)) {
151 3
            return http_build_query($query);
152
        }
153
154 1
        return null;
155
    }
156
}
157