Completed
Push — master ( 390a97...c4c478 )
by Daniel
9s
created

Capabilities   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 51
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B create() 0 24 2
A getSupportedComparators() 0 4 1
A canSetParent() 0 4 1
A canQueryCount() 0 4 1
1
<?php
2
3
namespace Psi\Component\ObjectAgent;
4
5
final class Capabilities
6
{
7
    private $supportedComparators;
8
    private $setParent;
9
    private $queryCount;
10
11
    private function __construct()
12
    {
13
        // this class cannot be instantiated with "new".
14
    }
15
16
    public static function create(array $capabilities): Capabilities
17
    {
18
        $defaults = [
19
            'supported_comparators' => [],
20
            'can_set_parent' => false,
21
            'can_query_count' => false,
22
        ];
23
24
        if ($diff = array_diff(array_keys($capabilities), array_keys($defaults))) {
25
            throw new \InvalidArgumentException(sprintf(
26
                'Unknown capabilities: "%s". Valid capabilities: "%s"',
27
                implode('", "', $diff), implode('", "', array_keys($defaults))
28
            ));
29
        }
30
31
        $capabilities = array_merge($defaults, $capabilities);
32
33
        $instance = new self();
34
        $instance->supportedComparators = (array) $capabilities['supported_comparators'];
35
        $instance->setParent = (bool) $capabilities['can_set_parent'];
36
        $instance->queryCount = (bool) $capabilities['can_query_count'];
37
38
        return $instance;
39
    }
40
41
    public function getSupportedComparators(): array
42
    {
43
        return $this->supportedComparators;
44
    }
45
46
    public function canSetParent(): bool
47
    {
48
        return $this->setParent;
49
    }
50
51
    public function canQueryCount(): bool
52
    {
53
        return $this->queryCount;
54
    }
55
}
56