FilterSpecResolver::resolve()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.3332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Noitran\RQL\Processors;
6
7
use Noitran\RQL\Contracts\Expression\ExprInterface;
8
use Noitran\RQL\Contracts\Processor\ProcessorInterface;
9
use Noitran\RQL\Contracts\Processor\SpecInterface;
10
use Noitran\RQL\Contracts\Resolver\ResolverInterface;
11
use Noitran\RQL\Exceptions\ExpressionException;
12
13
class FilterSpecResolver implements ResolverInterface
14
{
15
    /**
16
     * @var SpecInterface[]
17
     */
18
    protected $applicableSpecs = [];
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 12
    public function registerAll(array $filterSpecs): self
24
    {
25 12
        foreach ($filterSpecs as $spec) {
26 12
            $this->register($spec);
27
        }
28
29 12
        return $this;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 12
    public function register(string $filterStrategy): self
36
    {
37 12
        $this->applicableSpecs[] = new $filterStrategy();
38
39 12
        return $this;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 12
    public function resolve(ProcessorInterface $processor, ExprInterface $exprClass): SpecInterface
46
    {
47 12
        foreach ($this->applicableSpecs as $spec) {
48 12
            if ($spec->isSatisfiedBy($processor, $exprClass)) {
49 12
                return $spec;
50
            }
51
        }
52
53
        throw new ExpressionException(
54
            'Can\'t find how to apply operator defined in class: ' . \get_class($exprClass)
55
        );
56
    }
57
}
58