Passed
Push — master ( 6137ca...01151b )
by Kevin
02:21
created

CompositeNormalizer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 12
c 1
b 0
f 1
dl 0
loc 29
ccs 9
cts 10
cp 0.9
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 14 2
A classMethodMap() 0 5 1
1
<?php
2
3
namespace Zenstruck\Porpaginas\Doctrine\DBAL\Specification\Normalizer;
4
5
use Doctrine\DBAL\Query\Expression\CompositeExpression;
6
use Zenstruck\Porpaginas\Doctrine\DBAL\Specification\DBALContext;
7
use Zenstruck\Porpaginas\Specification\Logic\AndX;
8
use Zenstruck\Porpaginas\Specification\Logic\Composite;
9
use Zenstruck\Porpaginas\Specification\Logic\OrX;
10
use Zenstruck\Porpaginas\Specification\Normalizer;
11
use Zenstruck\Porpaginas\Specification\Normalizer\ClassMethodMap;
12
use Zenstruck\Porpaginas\Specification\Normalizer\NormalizerAware;
13
use Zenstruck\Porpaginas\Specification\Normalizer\WithNormalizer;
14
15
/**
16
 * @author Kevin Bond <[email protected]>
17
 */
18
final class CompositeNormalizer implements Normalizer, NormalizerAware
19
{
20
    use DBALNormalizer, WithNormalizer, ClassMethodMap;
21
22
    /**
23
     * @param Composite   $specification
24
     * @param DBALContext $context
25
     */
26 3
    public function normalize($specification, $context): ?CompositeExpression
27
    {
28 3
        $children = \array_filter(\array_map(
29
            function($child) use ($context) {
30 3
                return $this->normalizer()->normalize($child, $context);
31 3
            },
32 3
            $specification->children()
33
        ));
34
35 3
        if (empty($children)) {
36
            return null;
37
        }
38
39 3
        return $context->qb()->expr()->{self::methodFor($specification)}(...$children);
40
    }
41
42 6
    protected static function classMethodMap(): array
43
    {
44
        return [
45 6
            AndX::class => 'andX',
46
            OrX::class => 'orX',
47
        ];
48
    }
49
}
50