summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Cluster/Settings.php
blob: 8166dda34fb0502785c8fa422dc6a939158f69b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php

namespace Elastica\Cluster;

use Elastica\Client;
use Elastica\Request;

/**
 * Cluster settings
 *
 * @category Xodoa
 * @package  Elastica
 * @author   Nicolas Ruflin <spam@ruflin.com>
 * @link     http://www.elasticsearch.org/guide/reference/api/admin-cluster-update-settings.html
 */
class Settings
{
    /**
     * Client
     *
     * @var \Elastica\Client Client object
     */
    protected $_client = null;

    /**
     * Creates a cluster object
     *
     * @param \Elastica\Client $client Connection client object
     */
    public function __construct(Client $client)
    {
        $this->_client = $client;
    }

    /**
     * Returns settings data
     *
     * @return array Settings data (persistent and transient)
     */
    public function get()
    {
        return $this->request()->getData();
    }

    /**
     * Returns the current persistent settings of the cluster
     *
     * If param is set, only specified setting is return.
     *
     * @param  string $setting OPTIONAL Setting name to return
     * @return array|string|null Settings data
     */
    public function getPersistent($setting = '')
    {
        $data = $this->get();
        $settings = $data['persistent'];

        if (!empty($setting)) {
            if (isset($settings[$setting])) {
                return $settings[$setting];
            } else {
                return null;
            }
        }

        return $settings;
    }

    /**
     * Returns the current transient settings of the cluster
     *
     * If param is set, only specified setting is return.
     *
     * @param  string $setting OPTIONAL Setting name to return
     * @return array|string|null Settings data
     */
    public function getTransient($setting = '')
    {
        $data = $this->get();
        $settings = $data['transient'];

        if (!empty($setting)) {
            if (isset($settings[$setting])) {
                return $settings[$setting];
            } else {
                if (strpos($setting, '.') !== false) {
                    // convert dot notation to nested arrays
                    $keys = explode('.', $setting);
                    foreach ($keys as $key) {
                        if (isset($settings[$key])) {
                            $settings = $settings[$key];
                        } else {
                            return null;
                        }
                    }
                    return $settings;
                }
                return null;
            }
        }

        return $settings;
    }

    /**
     * Sets persistent setting
     *
     * @param  string $key
     * @param  string $value
     * @return \Elastica\Response
     */
    public function setPersistent($key, $value)
    {
        return $this->set(
            array(
                'persistent' => array(
                    $key => $value
                )
            )
        );
    }

    /**
     * Sets transient settings
     *
     * @param  string $key
     * @param  string $value
     * @return \Elastica\Response
     */
    public function setTransient($key, $value)
    {
        return $this->set(
            array(
                'transient' => array(
                    $key => $value
                )
            )
        );
    }

    /**
     * Sets the cluster to read only
     *
     * Second param can be used to set it persistent
     *
     * @param  bool $readOnly
     * @param  bool $persistent
     * @return \Elastica\Response $response
     */
    public function setReadOnly($readOnly = true, $persistent = false)
    {
        $key = 'cluster.blocks.read_only';

        if ($persistent) {
            $response = $this->setPersistent($key, $readOnly);
        } else {
            $response = $this->setTransient($key, $readOnly);
        }

        return $response;
    }

    /**
     * Set settings for cluster
     *
     * @param  array $settings Raw settings (including persistent or transient)
     * @return \Elastica\Response
     */
    public function set(array $settings)
    {
        return $this->request($settings, Request::PUT);
    }

    /**
     * Get the client
     *
     * @return \Elastica\Client
     */
    public function getClient()
    {
        return $this->_client;
    }

    /**
     * Sends settings request
     *
     * @param  array $data OPTIONAL Data array
     * @param  string $method OPTIONAL Transfer method (default = \Elastica\Request::GET)
     * @return \Elastica\Response Response object
     */
    public function request(array $data = array(), $method = Request::GET)
    {
        $path = '_cluster/settings';

        return $this->getClient()->request($path, $method, $data);
    }
}