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

PlayersQuery   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 48
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 4 1
A forPlayerIds() 0 5 1
A toQueryString() 0 13 3
A addCriterion() 0 11 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