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

TeamsQuery   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 54
ccs 24
cts 24
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 4 1
A forPlayerIds() 0 5 1
A forSeason() 0 5 1
A toQueryString() 0 13 3
A addCriterion() 0 11 3
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 5
    private function __construct()
17
    {
18 5
    }
19
20 5
    public static function create(): self
21
    {
22 5
        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 2
    public function forSeason(int $season)
32
    {
33 2
        $this->addCriterion(new SeasonCriterion($season));
34 2
        return $this;
35
    }
36
37 4
    public function toQueryString(): ?string
38
    {
39 4
        $query = [];
40 4
        foreach ($this->criteria as $criterion) {
41 3
            $query = array_merge($query, $criterion->toArray());
42
        }
43
44 4
        if (!empty($query)) {
45 3
            return http_build_query($query);
46
        }
47
48 1
        return null;
49
    }
50
51 4
    private function addCriterion(CriterionInterface $criterionToAdd): void
52
    {
53 4
        $criterionToAdd->checkCollisionWithCriteria($this->criteria);
54 4
        foreach ($this->criteria as $criterion) {
55 2
            if (get_class($criterion) === get_class($criterionToAdd)) {
56 2
                throw InvalidQueryException::sameCriteria($criterionToAdd);
57
            }
58
        }
59 4
        $this->criteria[] = $criterionToAdd;
60 4
        return;
61
    }
62
}
63