Test Failed
Push — master ( 04acc6...e7cb34 )
by Gerhard
11:27
created

BooleanType::buildQuery()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 24
cp 0
rs 8.8817
c 0
b 0
f 0
cc 6
nc 6
nop 3
crap 42
1
<?php
2
/**
3
 * TextFilter.php
4
 *
5
 * @since 19/01/17
6
 * @author gseidel
7
 */
8
9
namespace Enhavo\Bundle\AppBundle\Filter\Type;
10
11
use Enhavo\Bundle\AppBundle\Exception\FilterException;
12
use Enhavo\Bundle\AppBundle\Filter\AbstractFilterType;
13
use Enhavo\Bundle\AppBundle\Filter\FilterQuery;
14
use Symfony\Component\OptionsResolver\OptionsResolver;
15
16
class BooleanType extends AbstractFilterType
17
{
18
    const VALUE_TRUE = 1;
19
    const VALUE_FALSE = 2;
20
21
    public function createViewData($options, $name)
22
    {
23
        $data = [
24
            'type' => $this->getType(),
25
            'key' => $name,
26
            'value' => null,
27
            'initialValue' => null,
28
            'component' => $options['component'],
29
            'label' => $this->getLabel($options),
30
        ];
31
32
        if (!$options['checkbox']) {
33
            if ($data['component'] === 'filter-boolean') {
34
                $data['component'] = 'filter-option';
35
            }
36
            $data['choices'] = $this->getChoices($options);
37
        }
38
39
        return $data;
40
    }
41
42
    private function getChoices($options)
43
    {
44
        return [
45
            [
46
                'label' => $this->translator->trans($options['label_true'], [], $options['translation_domain']),
47
                'code' => self::VALUE_TRUE
48
            ],
49
            [
50
                'label' => $this->translator->trans($options['label_false'], [], $options['translation_domain']),
51
                'code' => self::VALUE_FALSE
52
            ]
53
        ];
54
    }
55
56
    public function buildQuery(FilterQuery $query, $options, $value)
57
    {
58
        if($value === null) {
59
            return;
60
        }
61
        $property = $options['property'];
62
63
        if ($options['checkbox']) {
64
            $boolValue = (boolean)$value;
65
            if($boolValue) {
66
                $equals = $options['equals'];
67
                $query->addWhere($property, FilterQuery::OPERATOR_EQUALS, $equals);
68
            }
69
        } else {
70
            if ($value == self::VALUE_TRUE) {
71
                $boolValue = true;
72
            } elseif ($value == self::VALUE_FALSE) {
73
                $boolValue = false;
74
            } else {
75
                throw new FilterException('Value invalid, must be one of ' . implode(',', [self::VALUE_TRUE, self::VALUE_FALSE]));
76
            }
77
78
            $property = $options['property'];
79
            $query->addWhere($property, FilterQuery::OPERATOR_EQUALS, $boolValue);
80
        }
81
    }
82
83
    public function configureOptions(OptionsResolver $optionsResolver)
84
    {
85
        parent::configureOptions($optionsResolver);
86
        $optionsResolver->setDefaults([
87
            'equals' => true,
88
            'component' => 'filter-boolean',
89
            'checkbox' => true,
90
            'label_true' => 'filter.boolean.label_true',
91
            'label_false' => 'filter.boolean.label_false',
92
            'translation_domain' => 'EnhavoAppBundle'
93
        ]);
94
    }
95
96
    public function getType()
97
    {
98
        return 'boolean';
99
    }
100
}
101