CustomOrFilter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
eloc 32
c 3
b 0
f 0
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B filterProperty() 0 42 8
A getDescription() 0 9 1
1
<?php
2
3
namespace ControleOnline\Filter;
4
5
use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Doctrine\Orm\Filter\AbstractFilter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Doctrine\Orm...yNameGeneratorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use ApiPlatform\Metadata\Operation;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Metadata\Operation was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Doctrine\ORM\QueryBuilder;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\QueryBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class CustomOrFilter extends AbstractFilter
11
{
12
    public function getDescription(string $resourceClass): array
13
    {
14
        return [
15
            'search' => [
16
                'property' => null,
17
                'type' => 'string',
18
                'required' => false,
19
                'openapi' => [
20
                    'description' => 'Search across multiple fields',
21
                ],
22
            ]
23
        ];
24
    }
25
26
    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
27
    {
28
        if ($property !== 'search') {
29
            return;
30
        }
31
32
        $alias = $queryBuilder->getRootAliases()[0];
33
        $andWhere = '';
34
        $relations = [];
35
36
        foreach ($this->properties as $property => $propVal) {
37
            $relation = explode('.', $property);
38
39
            if (count($relation) > 1) {
40
                if (!array_key_exists($relation[0], $relations)) {
41
                    $relations[$relation[0]] = uniqid();
42
                    $queryBuilder->leftJoin(sprintf('%s.%s', $alias, $relation[0]), 'i');
43
                }
44
45
                $queryBuilder->orWhere(sprintf('%s.%s LIKE :search', 'i', $relation[1]));
46
                $queryBuilder->setParameter('search', '%' . $value . '%');
47
                continue;
48
            }
49
50
            $andWhere .= sprintf('%s.%s LIKE :search', $alias, $property);
51
52
            next($this->properties);
53
            $nextKey = key($this->properties);
54
55
            if ($nextKey !== null && !strpos($nextKey, '.') !== false) {
56
                $andWhere .= ' OR ';
57
            }
58
        }
59
60
61
        if (empty($relations)) {
62
            $queryBuilder->andWhere($andWhere);
63
        } else {
64
            $queryBuilder->orWhere($andWhere);
65
        }
66
67
        $queryBuilder->setParameter('search', '%' . $value . '%');
68
    }
69
}
70