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

BooleanFilter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 15 4
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
        $column = $context->getField()->getOriginName();
34
35 2
        $condition = (int) $condition;
36 2
        $qb->andWhere("{$alias}.{$column} = $condition");
37 2
    }
38
}
39