Completed
Pull Request — master (#21)
by Pavel
21:56
created

NestedCriteriaConfigurator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 36
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B configure() 0 19 5
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Criteria;
4
5
use Doctrine\Common\Collections\Criteria;
6
use ScayTrase\Api\Cruds\CriteriaConfiguratorInterface;
7
use ScayTrase\Api\Cruds\Exception\CriteriaConfigurationException;
8
use ScayTrase\Api\Cruds\Exception\NestedConfiguratorException;
9
10
final class NestedCriteriaConfigurator implements CriteriaConfiguratorInterface
11
{
12
    /** @var CriteriaConfiguratorInterface[] */
13
    private $filters;
14
15
    /**
16
     * ChainFilter constructor.
17
     *
18
     * @param CriteriaConfiguratorInterface[] $filters
19
     */
20 6
    public function __construct(array $filters)
21
    {
22 6
        $this->filters = $filters;
23 6
    }
24
25
    /** {@inheritdoc} */
26 6
    public function configure(string $fqcn, Criteria $criteria, $arguments)
27
    {
28 6
        if (!is_array($arguments)) {
29 2
            throw NestedConfiguratorException::invalidType('array', gettype($arguments));
30
        }
31
32 4
        $diff = array_keys(array_diff_key($arguments, $this->filters));
33 4
        if (count($diff) !== 0) {
34 2
            throw NestedConfiguratorException::unknown($diff);
35
        }
36
37 2
        foreach ((array)$arguments as $filter => $item) {
38
            try {
39 2
                $this->filters[$filter]->configure($fqcn, $criteria, $item);
40
            } catch (CriteriaConfigurationException $exception) {
41 2
                throw NestedConfiguratorException::invalidNesting($filter, $exception);
42
            }
43
        }
44 2
    }
45
}
46