TeamsQuery::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PtrTn\Battlerite\Query\Teams;
4
5
use PtrTn\Battlerite\Exception\InvalidQueryException;
6
use PtrTn\Battlerite\Query\CriterionInterface;
7
use PtrTn\Battlerite\Query\QueryInterface;
8
9
class TeamsQuery 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 2
    public function forPlayerIds(array $playerIds)
26
    {
27 2
        $this->addCriterion(new PlayerIdsCriterion($playerIds));
28 2
        return $this;
29
    }
30
31 1
    public function forSeason(int $season)
32
    {
33 1
        $this->addCriterion(new SeasonCriterion($season));
34 1
        return $this;
35
    }
36
37 3
    public function toQueryString(): ?string
38
    {
39 3
        $query = [];
40 3
        foreach ($this->criteria as $criterion) {
41 2
            $query = array_merge($query, $criterion->toArray());
42
        }
43
44 3
        if (!empty($query)) {
45 2
            return http_build_query($query);
46
        }
47
48 1
        return null;
49
    }
50
51 3
    private function addCriterion(CriterionInterface $criterionToAdd): void
52
    {
53 3
        $criterionToAdd->checkCollisionWithCriteria($this->criteria);
54 3
        foreach ($this->criteria as $criterion) {
55 1
            if (get_class($criterion) === get_class($criterionToAdd)) {
56 1
                throw InvalidQueryException::sameCriteria($criterionToAdd);
57
            }
58
        }
59 3
        $this->criteria[] = $criterionToAdd;
60 3
        return;
61
    }
62
}
63