DynamicFieldsRegistry   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 36
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 7 2
A set() 0 4 1
A has() 0 4 1
A get() 0 4 1
A getAll() 0 8 2
1
<?php
2
3
namespace Nayjest\Querying\Row;
4
5
use Exception;
6
7
class DynamicFieldsRegistry
8
{
9
    private $dynamicFields = [];
10
11 3
    public function add($name, callable $fn)
12
    {
13 3
        if ($this->has($name)) {
14
            throw new Exception("Dynamic field $name already defined");
15
        }
16 3
        $this->set($name, $fn);
17 3
    }
18
19 3
    public function set($name, callable $fn)
20
    {
21 3
        $this->dynamicFields[$name] = $fn;
22 3
    }
23
24 3
    public function has($name)
25
    {
26 3
        return array_key_exists($name, $this->dynamicFields);
27
    }
28
29 3
    public function get($name, RowInterface $row)
30
    {
31 3
        return call_user_func($this->dynamicFields[$name], $row);
32
    }
33
34 2
    public function getAll(RowInterface $row)
35
    {
36 2
        $res = [];
37 2
        foreach(array_keys($this->dynamicFields) as $name) {
38 2
            $res[$name] = $this->get($name, $row);
39 2
        }
40 2
        return $res;
41
    }
42
}
43