Passed
Push — master ( 22bce8...5a27f9 )
by Igor
03:05
created

Settings::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace ClickHouseDB;
4
5
use ClickHouseDB\Transport\Http;
6
7
class Settings
8
{
9
    /**
10
     * @var Http
11
     */
12
    private $client = null;
13
14
    /**
15
     * @var array
16
     */
17
    private $settings = [];
18
19
    private $_ReadOnlyUser=false;
20
21
    /**
22
     * @var bool
23
     */
24
    private $_isHttps=false;
0 ignored issues
show
introduced by
The private property $_isHttps is not used, and could be removed.
Loading history...
25
26
    /**
27
     * Settings constructor.
28
     * @param Http $client
29
     */
30 44
    public function __construct(Http $client)
31
    {
32
        $default = [
33 44
            'extremes'                => false,
34
            'readonly'                => true,
35
            'max_execution_time'      => 20,
36
            'enable_http_compression' => 0,
37
            'https'                   => false
38
        ];
39
40 44
        $this->settings = $default;
41 44
        $this->client = $client;
42 44
    }
43
44
    /**
45
     * @param string|int $key
46
     * @return mixed
47
     */
48 36
    public function get($key)
49
    {
50 36
        if (!$this->is($key)) return null;
51 36
        return $this->settings[$key];
52
    }
53
54
    /**
55
     * @param string|int $key
56
     * @return bool
57
     */
58 36
    public function is($key)
59
    {
60 36
        return isset($this->settings[$key]);
61
    }
62
63
64
    /**
65
     * @param string|int $key
66
     * @param mixed $value
67
     * @return $this
68
     */
69 44
    public function set($key, $value)
70
    {
71 44
        $this->settings[$key] = $value;
72 44
        return $this;
73
    }
74
75
    /**
76
     * @return mixed
77
     */
78 1
    public function getDatabase()
79
    {
80 1
        return $this->get('database');
81
    }
82
83
    /**
84
     * @param string $db
85
     * @return $this
86
     */
87 44
    public function database($db)
88
    {
89 44
        $this->set('database', $db);
90 44
        return $this;
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96 36
    public function getTimeOut()
97
    {
98 36
        return $this->get('max_execution_time');
99
    }
100
101
    /**
102
     * @return mixed|null
103
     */
104 36
    public function isEnableHttpCompression()
105
    {
106 36
        return $this->getSetting('enable_http_compression');
107
    }
108
109
    /**
110
     * @param bool|int $flag
111
     * @return $this
112
     */
113 27
    public function enableHttpCompression($flag)
114
    {
115 27
        $this->set('enable_http_compression', intval($flag));
116 27
        return $this;
117
    }
118
119
120
    public function https($flag=true)
121
    {
122
        $this->set('https', $flag);
123
        return $this;
124
    }
125
126 36
    public function isHttps()
127
    {
128 36
        return $this->get('https');
129
    }
130
131
132
    /**
133
     * @param int|bool $flag
134
     * @return $this
135
     */
136
    public function readonly($flag)
137
    {
138
        $this->set('readonly', $flag);
139
        return $this;
140
    }
141
142
    /**
143
     * @param string $session_id
144
     * @return $this
145
     */
146 2
    public function session_id($session_id)
147
    {
148 2
        $this->set('session_id', $session_id);
149 2
        return $this;
150
    }
151
    /**
152
     * @return mixed
153
     */
154 36
    public function getSessionId()
155
    {
156 36
        if (empty($this->settings['session_id'])) return false;
157 2
        return $this->get('session_id');
158
    }
159
160
    /**
161
     * @return string
162
     */
163 2
    public function makeSessionId()
164
    {
165 2
        $this->session_id(sha1(uniqid('', true)));
166 2
        return $this->getSessionId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getSessionId() could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
167
    }
168
169
    /**
170
     * @param int $time
171
     * @return $this
172
     */
173 2
    public function max_execution_time($time)
174
    {
175 2
        $this->set('max_execution_time', $time);
176 2
        return $this;
177
    }
178
179
    /**
180
     * @return array
181
     */
182 36
    public function getSettings()
183
    {
184 36
        return $this->settings;
185
    }
186
187
    /**
188
     * @param array $settings_array
189
     * @return $this
190
     */
191 2
    public function apply($settings_array)
192
    {
193 2
        foreach ($settings_array as $key => $value) {
194 2
            $this->set($key, $value);
195
        }
196
197 2
        return $this;
198
    }
199
200
    /**
201
     * @param int|bool $flag
202
     */
203
    public function setReadOnlyUser($flag)
204
    {
205
        $this->_ReadOnlyUser=$flag;
206
    }
207
208
    /**
209
     * @return bool
210
     */
211 36
    public function isReadOnlyUser()
212
    {
213 36
        return $this->_ReadOnlyUser;
214
    }
215
216
    /**
217
     * @param string $name
218
     * @return mixed|null
219
     */
220 36
    public function getSetting($name)
221
    {
222 36
        if (!isset($this->settings[$name])) {
223
            return null;
224
        }
225
226 36
        return $this->get($name);
227
    }
228
}
229