Passed
Push — master ( 75634d...701667 )
by Peter
02:12
created

PlayersQuery::forPlayerNames()   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
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PtrTn\Battlerite\Query;
4
5
use PtrTn\Battlerite\Exception\InvalidQueryException;
6
use PtrTn\Battlerite\Query\Criterion\CriterionInterface;
7
use PtrTn\Battlerite\Query\Criterion\PlayerIdsCriterion;
8
use PtrTn\Battlerite\Query\Criterion\PlayerNamesCriterion;
9
use PtrTn\Battlerite\Query\Criterion\SteamIdsCriterion;
10
11
class PlayersQuery implements QueryInterface
12
{
13
    /**
14
     * @var CriterionInterface[]
15
     */
16
    private $criteria = [];
17
18 6
    private function __construct()
19
    {
20 6
    }
21
22 6
    public static function create(): self
23
    {
24 6
        return new self();
25
    }
26
27 3
    public function forPlayerIds(array $playerIds)
28
    {
29 3
        $this->addCriterion(new PlayerIdsCriterion($playerIds));
30 3
        return $this;
31
    }
32
33 1
    public function forSteamIds(array $steamIds)
34
    {
35 1
        $this->addCriterion(new SteamIdsCriterion($steamIds));
36 1
        return $this;
37
    }
38
39 1
    public function forPlayerNames(array $playerNames)
40
    {
41 1
        $this->addCriterion(new PlayerNamesCriterion($playerNames));
42 1
        return $this;
43
    }
44
45 5
    public function toQueryString(): ?string
46
    {
47 5
        $query = [];
48 5
        foreach ($this->criteria as $criterion) {
49 4
            $query = array_merge($query, $criterion->toArray());
50
        }
51
52 5
        if (!empty($query)) {
53 4
            return http_build_query($query);
54
        }
55
56 1
        return null;
57
    }
58
59 5
    private function addCriterion(CriterionInterface $criterionToAdd): void
60
    {
61 5
        $criterionToAdd->checkCollisionWithCriteria($this->criteria);
62 5
        foreach ($this->criteria as $criterion) {
63 1
            if (get_class($criterion) === get_class($criterionToAdd)) {
64 1
                throw InvalidQueryException::sameCriteria($criterionToAdd);
65
            }
66
        }
67 5
        $this->criteria[] = $criterionToAdd;
68 5
        return;
69
    }
70
}
71