Completed
Pull Request — master (#348)
by
unknown
10:14
created

AbstractSearchEndpoint   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 87
rs 10
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\ParametersTrait;
18
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
19
use ONGR\ElasticsearchDSL\Serializer\Normalizer\AbstractNormalizable;
20
21
/**
22
 * Abstract class used to define search endpoint with references.
23
 */
24
abstract class AbstractSearchEndpoint extends AbstractNormalizable implements SearchEndpointInterface
25
{
26
    use ParametersTrait;
27
28
    private array $container = [];
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 T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
29
30
    public function add(BuilderInterface $builder, string $key = null): ?string
31
    {
32
        if (array_key_exists($key, $this->container)) {
33
            throw new \OverflowException(sprintf('Builder with %s name for endpoint has already been added!', $key));
34
        }
35
36
        if (!$key) {
37
            $key = bin2hex(random_bytes(30));
38
        }
39
40
        $this->container[$key] = $builder;
41
42
        return $key;
43
    }
44
45
    public function addToBool(BuilderInterface $builder, ?string $boolType = null, ?string $key = null): string
46
    {
47
        throw new \BadFunctionCallException(sprintf("Endpoint %s doesn't support bool statements", static::NAME));
48
    }
49
50
    public function remove(string $key): static
51
    {
52
        if ($this->has($key)) {
53
            unset($this->container[$key]);
54
        }
55
56
        return $this;
57
    }
58
59
    public function has(string $key): bool
60
    {
61
        return array_key_exists($key, $this->container);
62
    }
63
64
    public function get(string $key): ?BuilderInterface
65
    {
66
        if ($this->has($key)) {
67
            return $this->container[$key];
68
        }
69
70
        return null;
71
    }
72
73
    public function getAll(?string $boolType = null): array
74
    {
75
        return $this->container;
76
    }
77
78
    public function getBool(): ?BoolQuery
79
    {
80
        throw new \BadFunctionCallException(sprintf("Endpoint %s doesn't support bool statements", static::NAME));
81
    }
82
}
83