Completed
Push — master ( d3ac62...0cb203 )
by Federico
02:08
created

lib/Elastica/Cluster/Settings.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Elastica\Cluster;
4
5
use Elastica\Client;
6
use Elastica\Request;
7
8
/**
9
 * Cluster settings.
10
 *
11
 * @author   Nicolas Ruflin <[email protected]>
12
 *
13
 * @see     https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-update-settings.html
14
 */
15
class Settings
16
{
17
    /**
18
     * @var \Elastica\Client Client object
19
     */
20
    protected $_client = null;
21
22
    /**
23
     * Creates a cluster object.
24
     *
25
     * @param \Elastica\Client $client Connection client object
26
     */
27
    public function __construct(Client $client)
28
    {
29
        $this->_client = $client;
30
    }
31
32
    /**
33
     * Returns settings data.
34
     *
35
     * @return array Settings data (persistent and transient)
36
     */
37
    public function get()
38
    {
39
        return $this->request()->getData();
40
    }
41
42
    /**
43
     * Returns the current persistent settings of the cluster.
44
     *
45
     * If param is set, only specified setting is return.
46
     *
47
     * @param string $setting OPTIONAL Setting name to return
48
     *
49
     * @return array|string|null Settings data
50
     */
51
    public function getPersistent($setting = '')
52
    {
53
        $data = $this->get();
54
        $settings = $data['persistent'];
55
56
        if (!empty($setting)) {
57
            if (isset($settings[$setting])) {
58
                return $settings[$setting];
59
            }
60
61
            return;
62
        }
63
64
        return $settings;
65
    }
66
67
    /**
68
     * Returns the current transient settings of the cluster.
69
     *
70
     * If param is set, only specified setting is return.
71
     *
72
     * @param string $setting OPTIONAL Setting name to return
73
     *
74
     * @return array|string|null Settings data
75
     */
76
    public function getTransient($setting = '')
77
    {
78
        $data = $this->get();
79
        $settings = $data['transient'];
80
81
        if (!empty($setting)) {
82
            if (isset($settings[$setting])) {
83
                return $settings[$setting];
84
            }
85
86 View Code Duplication
            if (false !== strpos($setting, '.')) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
                // convert dot notation to nested arrays
88
                $keys = explode('.', $setting);
89
                foreach ($keys as $key) {
90
                    if (isset($settings[$key])) {
91
                        $settings = $settings[$key];
92
                    } else {
93
                        return;
94
                    }
95
                }
96
97
                return $settings;
98
            }
99
100
            return;
101
        }
102
103
        return $settings;
104
    }
105
106
    /**
107
     * Sets persistent setting.
108
     *
109
     * @param string $key
110
     * @param string $value
111
     *
112
     * @return \Elastica\Response
113
     */
114
    public function setPersistent($key, $value)
115
    {
116
        return $this->set(
117
            [
118
                'persistent' => [
119
                    $key => $value,
120
                ],
121
            ]
122
        );
123
    }
124
125
    /**
126
     * Sets transient settings.
127
     *
128
     * @param string $key
129
     * @param string $value
130
     *
131
     * @return \Elastica\Response
132
     */
133
    public function setTransient($key, $value)
134
    {
135
        return $this->set(
136
            [
137
                'transient' => [
138
                    $key => $value,
139
                ],
140
            ]
141
        );
142
    }
143
144
    /**
145
     * Sets the cluster to read only.
146
     *
147
     * Second param can be used to set it persistent
148
     *
149
     * @param bool $readOnly
150
     * @param bool $persistent
151
     *
152
     * @return \Elastica\Response $response
153
     */
154
    public function setReadOnly($readOnly = true, $persistent = false)
155
    {
156
        $key = 'cluster.blocks.read_only';
157
158
        return $persistent
159
            ? $this->setPersistent($key, $readOnly)
160
            : $this->setTransient($key, $readOnly);
161
    }
162
163
    /**
164
     * Set settings for cluster.
165
     *
166
     * @param array $settings Raw settings (including persistent or transient)
167
     *
168
     * @return \Elastica\Response
169
     */
170
    public function set(array $settings)
171
    {
172
        return $this->request($settings, Request::PUT);
173
    }
174
175
    /**
176
     * Get the client.
177
     *
178
     * @return \Elastica\Client
179
     */
180
    public function getClient()
181
    {
182
        return $this->_client;
183
    }
184
185
    /**
186
     * Sends settings request.
187
     *
188
     * @param array  $data   OPTIONAL Data array
189
     * @param string $method OPTIONAL Transfer method (default = \Elastica\Request::GET)
190
     *
191
     * @return \Elastica\Response Response object
192
     */
193
    public function request(array $data = [], $method = Request::GET)
194
    {
195
        $path = '_cluster/settings';
196
197
        return $this->getClient()->request($path, $method, $data);
198
    }
199
}
200