Passed
Push — master ( 38b002...67e4f3 )
by Rafael
05:37
created

SearchByDoctrineColumn::__invoke()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 21
cp 0
rs 9.4222
cc 5
nc 9
nop 7
crap 30
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\SearchBy\Common;
12
13
use Doctrine\ORM\Query\Expr\Andx;
14
use Doctrine\ORM\Query\Expr\Orx;
15
use Doctrine\ORM\QueryBuilder;
16
use Ynlo\GraphQLBundle\SearchBy\SearchByContext;
17
use Ynlo\GraphQLBundle\SearchBy\SearchByInterface;
18
19
class SearchByDoctrineColumn implements SearchByInterface
20
{
21
    /**
22
     * @inheritDoc
23
     */
24
    public function __invoke(SearchByContext $context, QueryBuilder $qb, Orx $orx, $alias, string $column, string $mode, string $search)
25
    {
26
        while (strpos($column, '.') !== false) {
27
            [$child, $column] = explode('.', $column, 2);
28
            $parentAlias = $alias;
29
            $alias = 'searchJoin'.ucfirst($child);
30
            if (!\in_array($alias, $qb->getAllAliases(), true)) {
31
                $qb->leftJoin("{$parentAlias}.{$child}", $alias);
32
            }
33
        }
34
35
        if (self::PARTIAL_SEARCH === $mode) {
36
            //search each word separate
37
            $searchArray = explode(' ', $search);
38
39
            $partialAnd = new Andx();
40
            foreach ($searchArray as $index => $q) {
41
                $partialAnd->add("$alias.$column LIKE :query_search_$index");
42
                $qb->setParameter("query_search_$index", '%'.addcslashes($q, '%_').'%');
43
            }
44
            $orx->add($partialAnd);
45
        } else {
46
            $orx->add("$alias.$column LIKE :query_search");
47
            $qb->setParameter('query_search', trim($search));
48
        }
49
    }
50
}