Completed
Pull Request — master (#348)
by
unknown
09:42 queued 08:20
created

QueryEndpoint   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 80
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ONGR\ElasticsearchDSL\SearchEndpoint;
15
16
use ONGR\ElasticsearchDSL\BuilderInterface;
17
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
18
use ONGR\ElasticsearchDSL\Serializer\Normalizer\OrderedNormalizerInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
21
class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerInterface
22
{
23
    public const NAME = 'query';
24
25
    private ?BoolQuery $bool = null;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '?', expecting T_FUNCTION or T_CONST
Loading history...
26
27
    private bool $filtersSet = false;
28
29
    public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = [])
30
    {
31
        if (!$this->filtersSet && $this->hasReference('filter_query')) {
32
            /** @var BuilderInterface $filter */
33
            $filter = $this->getReference('filter_query');
34
            $this->addToBool($filter, BoolQuery::FILTER);
35
            $this->filtersSet = true;
36
        }
37
38
        if (!$this->bool) {
39
            return null;
40
        }
41
42
        return $this->bool->toArray();
43
    }
44
45
    public function add(BuilderInterface $builder, string $key = null): string
46
    {
47
        return $this->addToBool($builder, BoolQuery::MUST, $key);
48
    }
49
50
    public function addToBool(BuilderInterface $builder, ?string $boolType = null, ?string $key = null): string
51
    {
52
        if (!$this->bool) {
53
            $this->bool = new BoolQuery();
54
        }
55
56
        return $this->bool->add($builder, $boolType, $key);
57
    }
58
59
    public function getOrder(): int
60
    {
61
        return 2;
62
    }
63
64
    public function getBool(): ?BoolQuery
65
    {
66
        return $this->bool;
67
    }
68
69
    public function getAll($boolType = null): array
70
    {
71
        return $this->bool->getQueries($boolType);
72
    }
73
}
74