PlayersQuery::addCriterion()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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