Passed
Push — master ( b73665...44e90f )
by Peter
32s
created

PlayersQuery   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 60
ccs 27
cts 27
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 4 1
A forPlayerIds() 0 5 1
A forSteamIds() 0 5 1
A forPlayerNames() 0 5 1
A toQueryString() 0 13 3
A addCriterion() 0 11 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 6
    private function __construct()
17
    {
18 6
    }
19
20 6
    public static function create(): self
21
    {
22 6
        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 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 5
    public function toQueryString(): ?string
44
    {
45 5
        $query = [];
46 5
        foreach ($this->criteria as $criterion) {
47 4
            $query = array_merge($query, $criterion->toArray());
48
        }
49
50 5
        if (!empty($query)) {
51 4
            return http_build_query($query);
52
        }
53
54 1
        return null;
55
    }
56
57 5
    private function addCriterion(CriterionInterface $criterionToAdd): void
58
    {
59 5
        $criterionToAdd->checkCollisionWithCriteria($this->criteria);
60 5
        foreach ($this->criteria as $criterion) {
61 1
            if (get_class($criterion) === get_class($criterionToAdd)) {
62 1
                throw InvalidQueryException::sameCriteria($criterionToAdd);
63
            }
64
        }
65 5
        $this->criteria[] = $criterionToAdd;
66 5
        return;
67
    }
68
}
69