Completed
Push — master ( 670f0c...07e238 )
by Pavel
12:30
created

NestedCriteriaConfigurator::configure()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 12
cp 0.9167
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 3
crap 5.0144
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 3
    public function __construct(array $filters)
21
    {
22 3
        $this->filters = $filters;
23 3
    }
24
25
    /** {@inheritdoc} */
26 3
    public function configure($fqcn, Criteria $criteria, $arguments)
27
    {
28 3
        if (!is_array($arguments)) {
29 1
            throw NestedConfiguratorException::invalidType('array', gettype($arguments));
30
        }
31
32 2
        $diff = array_keys(array_diff_key($arguments, $this->filters));
33 2
        if (count($diff) !== 0) {
34 1
            throw NestedConfiguratorException::unknown($diff);
35
        }
36
37 1
        foreach ((array)$arguments as $filter => $item) {
38
            try {
39 1
                $this->filters[$filter]->configure($fqcn, $criteria, $item);
40 1
            } catch (CriteriaConfigurationException $exception) {
41
                throw NestedConfiguratorException::invalidNesting($filter, $exception);
42
            }
43 1
        }
44 1
    }
45
}
46