Completed
Push — master ( b54569...becf03 )
by Rafael
09:38
created

BooleanFilter::resolveColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
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
17
class BooleanFilter implements FilterInterface
18
{
19
    /**
20
     * @inheritDoc
21
     */
22 4
    public function __invoke(FilterContext $context, QueryBuilder $qb, $condition)
23
    {
24 4
        if (!\is_bool($condition)) {
25 1
            throw new \RuntimeException('Invalid filter condition');
26
        }
27
28 3
        if (!$context->getField() || !$context->getField()->getName()) {
29 1
            throw new \RuntimeException('There are not valid field related to this filter.');
30
        }
31
32 2
        $alias = $qb->getRootAliases()[0];
33 2
        $condition = (int) $condition;
34 2
        $this->applyFilter($qb, $alias, $this->resolveColumn($context), $condition);
0 ignored issues
show
Bug introduced by
$condition of type integer is incompatible with the type boolean expected by parameter $condition of Ynlo\GraphQLBundle\Filte...anFilter::applyFilter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        $this->applyFilter($qb, $alias, $this->resolveColumn($context), /** @scrutinizer ignore-type */ $condition);
Loading history...
35
    }
36
37
    /**
38 2
     * @param FilterContext $context
39 2
     *
40 2
     * @return string
41
     */
42
    protected function resolveColumn(FilterContext $context): string
43
    {
44
        $column = $context->getField()->getOriginName();
45
        if ($context->getField()->getOriginType() === 'ReflectionMethod') {
46
            $column = $context->getField()->getName();
47
        }
48
49
        return $column;
50
    }
51
52
    /**
53
     * @param QueryBuilder $qb
54
     * @param string       $alias
55
     * @param string       $column
56
     * @param bool         $condition
57
     */
58
    protected function applyFilter(QueryBuilder $qb, $alias, $column, bool $condition): void
59
    {
60
        $value = (int) $condition;
61
        $qb->andWhere("{$alias}.{$column} = $value");
62
    }
63
}
64