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

PlayersQuery::addCriterion()   A

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