Completed
Push — master ( 5e8cea...8544a9 )
by Nicolas
01:42
created

Pipeline::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica;
4
5
use Elastica\Exception\InvalidException;
6
use Elastica\Processor\AbstractProcessor;
7
use Elasticsearch\Endpoints\AbstractEndpoint;
8
use Elasticsearch\Endpoints\Ingest\Pipeline\Delete;
9
use Elasticsearch\Endpoints\Ingest\Pipeline\Get;
10
use Elasticsearch\Endpoints\Ingest\Pipeline\Put;
11
12
/**
13
 * Elastica Pipeline object.
14
 *
15
 * Handles Pipeline management & definition.
16
 *
17
 * @author   Federico Panini <[email protected]>
18
 *
19
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest-processors.html
20
 */
21
class Pipeline extends Param
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $id;
27
28
    /**
29
     * @var Client Client object
30
     */
31
    protected $_client;
32
33
    /**
34
     * @var AbstractProcessor[]
35
     */
36
    protected $_processors = [];
37
38
    public function __construct(Client $client)
39
    {
40
        $this->_client = $client;
41
    }
42
43
    /**
44
     * Create a Pipeline.
45
     *
46
     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/put-pipeline-api.html
47
     */
48
    public function create(): Response
49
    {
50
        if (empty($this->id)) {
51
            throw new InvalidException('You should set a valid pipeline id');
52
        }
53
54
        if (empty($this->_params['description'])) {
55
            throw new InvalidException('You should set a valid processor description.');
56
        }
57
58
        if (empty($this->_processors['processors'])) {
59
            throw new InvalidException('You should set a valid processor of type Elastica\Processor\AbstractProcessor.');
60
        }
61
62
        $endpoint = new Put();
63
        $endpoint->setID($this->id);
64
        $endpoint->setBody($this->toArray());
65
66
        return $this->requestEndpoint($endpoint);
67
    }
68
69
    /**
70
     * Get a Pipeline Object.
71
     *
72
     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/get-pipeline-api.html
73
     */
74
    public function getPipeline(string $id): Response
75
    {
76
        $endpoint = new Get();
77
        $endpoint->setID($id);
78
79
        return $this->requestEndpoint($endpoint);
80
    }
81
82
    /**
83
     * Delete a Pipeline.
84
     *
85
     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/delete-pipeline-api.html
86
     */
87
    public function deletePipeline(string $id): Response
88
    {
89
        $endpoint = new Delete();
90
        $endpoint->setID($id);
91
92
        return $this->requestEndpoint($endpoint);
93
    }
94
95
    /**
96
     * Sets query as raw array. Will overwrite all already set arguments.
97
     *
98
     * @param array $processors array
99
     */
100
    public function setRawProcessors(array $processors): self
101
    {
102
        $this->_processors = $processors;
103
104
        return $this;
105
    }
106
107
    public function addProcessor(AbstractProcessor $processor): self
108
    {
109
        if (empty($this->_processors)) {
110
            $this->_processors['processors'] = $processor->toArray();
111
            $this->_params['processors'] = [];
112
        } else {
113
            $this->_processors['processors'] = \array_merge($this->_processors['processors'], $processor->toArray());
114
        }
115
116
        return $this;
117
    }
118
119
    public function setId(string $id): self
120
    {
121
        $this->id = $id;
122
123
        return $this;
124
    }
125
126
    public function getId(): ?string
127
    {
128
        return $this->id;
129
    }
130
131
    /**
132
     * @param AbstractProcessor[]
133
     */
134
    public function setProcessors(array $processors): self
135
    {
136
        $this->setParam('processors', [$processors]);
137
138
        return $this;
139
    }
140
141
    public function setDescription(string $description): self
142
    {
143
        $this->setParam('description', $description);
144
145
        return $this;
146
    }
147
148
    /**
149
     * Converts the params to an array. A default implementation exist to create
150
     * the an array out of the class name (last part of the class name)
151
     * and the params.
152
     */
153
    public function toArray(): array
154
    {
155
        $this->_params['processors'] = [$this->_processors['processors']];
156
157
        return $this->getParams();
158
    }
159
160
    public function getClient(): Client
161
    {
162
        return $this->_client;
163
    }
164
165
    /**
166
     * Makes calls to the elasticsearch server with usage official client Endpoint based on this index.
167
     */
168
    public function requestEndpoint(AbstractEndpoint $endpoint): Response
169
    {
170
        $cloned = clone $endpoint;
171
172
        return $this->getClient()->requestEndpoint($cloned);
173
    }
174
}
175