Completed
Push — master ( 2975e7...46c3ee )
by Rafael
07:49
created

ArrayFilter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 45
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 40 10
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Filter\Common;
12
13
use Doctrine\ORM\QueryBuilder;
14
use Ynlo\GraphQLBundle\Filter\FilterContext;
15
use Ynlo\GraphQLBundle\Filter\FilterInterface;
16
use Ynlo\GraphQLBundle\Model\Filter\ArrayComparisonExpression;
17
use Ynlo\GraphQLBundle\Type\NodeComparisonOperatorType;
18
19
/**
20
 * This filter is used to match values using the doctrine array column type
21
 */
22
class ArrayFilter implements FilterInterface
23
{
24
    /**
25
     * @inheritDoc
26
     */
27 5
    public function __invoke(FilterContext $context, QueryBuilder $qb, $condition)
28
    {
29 5
        if (!$condition instanceof ArrayComparisonExpression) {
30 1
            throw new \RuntimeException('Invalid filter condition');
31
        }
32
33 4
        if (!$context->getField() || !$context->getField()->getName()) {
34 1
            throw new \RuntimeException('There are not valid field related to this filter.');
35
        }
36
37 3
        $entity = $context->getNode()->getClass();
38 3
        $metadata = $qb->getEntityManager()->getClassMetadata($entity);
39 3
        $column = $context->getField()->getOriginName();
40
41 3
        if (!$metadata->hasField($column)) {
42 1
            throw new \RuntimeException(sprintf('There are not valid column in %s called %s.', $entity, $column));
43
        }
44
45 2
        $columnMapping = $metadata->getFieldMapping($column);
46
47 2
        $alias = $qb->getRootAliases()[0];
48 2
        switch ($condition->getOp()) {
49 2
            case NodeComparisonOperatorType::IN:
50 2
                foreach ($condition->getValues() as $value) {
51 2
                    switch ($columnMapping['type']) {
52 2
                        case 'array':
53 1
                        case 'json_array':
54 1
                            $qb->andWhere($qb->expr()->like("{$alias}.{$column}", "'%\"{$value}\"%'"));
55 1
                            break;
56 1
                        case 'simple_array':
57 1
                            $or = $qb->expr()->orX();
58 1
                            $or->add($qb->expr()->eq("{$alias}.{$column}", "'%$value%'")); // only one value
59 1
                            $or->add($qb->expr()->like("{$alias}.{$column}", "'$value,%'")); //first value
60 1
                            $or->add($qb->expr()->like("{$alias}.{$column}", "'%,$value,%'")); //middle value
61 1
                            $or->add($qb->expr()->like("{$alias}.{$column}", "'%,$value'")); // latest value
62 1
                            $qb->andWhere($or);
63 2
                            break;
64
                    }
65
                }
66 2
                break;
67
        }
68 2
    }
69
}
70