Passed
Push — main ( 3e8b61...019d3e )
by Степанов
02:00
created

Filter::getTableAliases()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 38
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 38
ccs 15
cts 15
cp 1
crap 5
rs 9.4888
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Atlance\HttpDbalFilter;
6
7
use Atlance\HttpDbalFilter\Query\Expression\Contract\ConditionFactoryInterface;
8
use Doctrine\DBAL\Query\QueryBuilder;
9
10
final class Filter
11
{
12 75
    public function __construct(private readonly ConditionFactoryInterface $factory)
13
    {
14 75
    }
15
16 73
    public function apply(QueryBuilder $qb, Query\Configuration $configuration): void
17
    {
18 73
        $this->select($qb, $configuration->filter)->order($qb, $configuration->order);
19
    }
20
21
    /**
22
     * @param array<string,array<string,array<int,string|int|float|\Stringable>>> $conditions
23
     */
24 73
    private function select(QueryBuilder $qb, array $conditions): self
25
    {
26 73
        foreach ($conditions as $expr => $aliases) {
27 72
            foreach ($aliases as $alias => $values) {
28 72
                $this->andWhere($qb, $this->factory->create($qb, $alias, $expr), $values);
29
            }
30
        }
31
32 64
        return $this;
33
    }
34
35
    /**
36
     * @param array<string,string> $conditions
37
     */
38 64
    private function order(QueryBuilder $qb, array $conditions): void
39
    {
40 64
        foreach ($conditions as $alias => $value) {
41 1
            $this->andWhere($qb, $this->factory->create($qb, $alias, 'order_by'), [$value]);
42
        }
43
    }
44
45
    /**
46
     * @param array<int,string|int|float|\Stringable> $values
47
     */
48 71
    private function andWhere(QueryBuilder $qb, Query\Expression\Condition $condition, array $values): void
49
    {
50 71
        (new Query\Builder($qb))->andWhere($condition->setValues($values));
51
    }
52
}
53