AbstractSearchEndpoint::remove()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
namespace ONGR\ElasticsearchDSL\SearchEndpoint;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use ONGR\ElasticsearchDSL\ParametersTrait;
16
use ONGR\ElasticsearchDSL\Serializer\Normalizer\AbstractNormalizable;
17
18
/**
19
 * Abstract class used to define search endpoint with references.
20
 */
21
abstract class AbstractSearchEndpoint extends AbstractNormalizable implements SearchEndpointInterface
22
{
23
    use ParametersTrait;
24
25
    /**
26
     * @var BuilderInterface[]
27
     */
28
    private $container = [];
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function add(BuilderInterface $builder, $key = null)
34
    {
35
        if (array_key_exists($key, $this->container)) {
36
            throw new \OverflowException(sprintf('Builder with %s name for endpoint has already been added!', $key));
37
        }
38
39
        if (!$key) {
40
            $key = bin2hex(random_bytes(30));
41
        }
42
43
        $this->container[$key] = $builder;
44
45
        return $key;
0 ignored issues
show
Bug Compatibility introduced by
The expression return $key; of type string|array is incompatible with the return type declared by the interface ONGR\ElasticsearchDSL\Se...hEndpointInterface::add of type string as it can also be of type array which is not included in this return type.
Loading history...
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function addToBool(BuilderInterface $builder, $boolType = null, $key = null)
52
    {
53
        throw new \BadFunctionCallException(sprintf("Endpoint %s doesn't support bool statements", static::NAME));
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function remove($key)
60
    {
61
        if ($this->has($key)) {
62
            unset($this->container[$key]);
63
        }
64
65
        return $this;
66
    }
67
68
    /**
69
     * Checks if builder with specific key exists.
70
     *
71
     * @param string $key Key to check if it exists in container.
72
     *
73
     * @return bool
74
     */
75
    public function has($key)
76
    {
77
        return array_key_exists($key, $this->container);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function get($key)
84
    {
85
        if ($this->has($key)) {
86
            return $this->container[$key];
87
        }
88
89
        return null;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getAll($boolType = null)
96
    {
97
        return $this->container;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getBool()
104
    {
105
        throw new \BadFunctionCallException(sprintf("Endpoint %s doesn't support bool statements", static::NAME));
106
    }
107
}
108