DynamicFieldsRegistry::add()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
nc 2
cc 2
eloc 4
nop 2
crap 2.032
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