Passed
Push — master ( 1606b5...4950b0 )
by Igor
02:22
created

Settings::session_id()   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 1
crap 1
1
<?php
0 ignored issues
show
introduced by
Missing declare(strict_types = 1).
Loading history...
2
3
namespace ClickHouseDB;
4
5
use ClickHouseDB\Transport\Http;
6
7
class Settings
8
{
9
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Settings::$client with single line content, use one-line comment instead.
Loading history...
10
     * @var Http
11
     */
12
    private $client = null;
0 ignored issues
show
introduced by
Class Settings contains write-only property $client.
Loading history...
13
14
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Settings::$settings with single line content, use one-line comment instead.
Loading history...
15
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Settings::$settings does not specify type hint for its items.
Loading history...
16
     */
17
    private $settings = [];
18
19
    private $_ReadOnlyUser = false;
0 ignored issues
show
introduced by
Property \ClickHouseDB\Settings::$_ReadOnlyUser does not have @var annotation.
Loading history...
20
21
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Settings::$_isHttps with single line content, use one-line comment instead.
Loading history...
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...
introduced by
Class Settings contains unused property $_isHttps.
Loading history...
25
26
    /**
27
     * Settings constructor.
0 ignored issues
show
introduced by
Documentation comment contains forbidden comment "Settings constructor.".
Loading history...
28
     * @param Http $client
0 ignored issues
show
introduced by
Method \ClickHouseDB\Settings::__construct() has useless @param annotation for parameter $client.
Loading history...
29
     */
30 54
    public function __construct(Http $client)
31
    {
32
        $default = [
33 54
            'extremes'                => false,
34
            'readonly'                => true,
35
            'max_execution_time'      => 20,
36
            'enable_http_compression' => 0,
37
            'https'                   => false
0 ignored issues
show
introduced by
Multiline arrays must have a trailing comma after the last element.
Loading history...
Coding Style introduced by
Each line in an array declaration must end in a comma
Loading history...
38
        ];
39
40 54
        $this->settings = $default;
41 54
        $this->client = $client;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
42 54
    }
43
44
    /**
45
     * @param string|int $key
46
     * @return mixed
47
     */
48 44
    public function get($key)
49
    {
50 44
        if (!$this->is($key)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
51
            return null;
52
        }
53 44
        return $this->settings[$key];
54
    }
55
56
    /**
57
     * @param string|int $key
58
     * @return bool
59
     */
60 44
    public function is($key)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Settings::is() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
61
    {
62 44
        return isset($this->settings[$key]);
63
    }
64
65
66
    /**
67
     * @param string|int $key
68
     * @param mixed $value
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
69
     * @return $this
70
     */
71 54
    public function set($key, $value)
72
    {
73 54
        $this->settings[$key] = $value;
74 54
        return $this;
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80 1
    public function getDatabase()
81
    {
82 1
        return $this->get('database');
83
    }
84
85
    /**
86
     * @param string $db
87
     * @return $this
88
     */
89 54
    public function database($db)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Settings::database() does not have parameter type hint for its parameter $db but it should be possible to add it based on @param annotation "string".
Loading history...
90
    {
91 54
        $this->set('database', $db);
92 54
        return $this;
93
    }
94
95
    /**
96
     * @return mixed
97
     */
98 36
    public function getTimeOut()
99
    {
100 36
        return $this->get('max_execution_time');
101
    }
102
103
    /**
104
     * @return mixed|null
105
     */
106 35
    public function isEnableHttpCompression()
107
    {
108 35
        return $this->getSetting('enable_http_compression');
109
    }
110
111
    /**
112
     * @param bool|int $flag
113
     * @return $this
114
     */
115 54
    public function enableHttpCompression($flag)
116
    {
117 54
        $this->set('enable_http_compression', intval($flag));
0 ignored issues
show
introduced by
Function intval() should not be referenced via a fallback global name, but via a use statement.
Loading history...
118 54
        return $this;
119
    }
120
121
122 1
    public function https($flag = true)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Settings::https() does not have parameter type hint nor @param annotation for its parameter $flag.
Loading history...
introduced by
Method \ClickHouseDB\Settings::https() does not have return type hint nor @return annotation for its return value.
Loading history...
123
    {
124 1
        $this->set('https', $flag);
125 1
        return $this;
126
    }
127
128 44
    public function isHttps()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Settings::isHttps() does not have return type hint nor @return annotation for its return value.
Loading history...
129
    {
130 44
        return $this->get('https');
131
    }
132
133
134
    /**
135
     * @param int|bool $flag
136
     * @return $this
137
     */
138
    public function readonly($flag)
139
    {
140
        $this->set('readonly', $flag);
141
        return $this;
142
    }
143
144
    /**
145
     * @param string $session_id
146
     * @return $this
147
     */
148 2
    public function session_id($session_id)
0 ignored issues
show
Coding Style introduced by
Method name "Settings::session_id" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Settings::session_id() does not have parameter type hint for its parameter $session_id but it should be possible to add it based on @param annotation "string".
Loading history...
149
    {
150 2
        $this->set('session_id', $session_id);
151 2
        return $this;
152
    }
153
    /**
154
     * @return mixed|bool
155
     */
156 36
    public function getSessionId()
157
    {
158 36
        if (empty($this->settings['session_id'])) {
159 36
            return false;
160
        }
161 2
        return $this->get('session_id');
162
    }
163
164
    /**
165
     * @return string|bool
166
     */
167 2
    public function makeSessionId()
168
    {
169 2
        $this->session_id(sha1(uniqid('', true)));
0 ignored issues
show
introduced by
Function sha1() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function uniqid() should not be referenced via a fallback global name, but via a use statement.
Loading history...
170 2
        return $this->getSessionId();
171
    }
172
173
    /**
174
     * @param int $time
175
     * @return $this
176
     */
177 2
    public function max_execution_time($time)
0 ignored issues
show
Coding Style introduced by
Method name "Settings::max_execution_time" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Settings::max_execution_time() does not have parameter type hint for its parameter $time but it should be possible to add it based on @param annotation "int".
Loading history...
178
    {
179 2
        $this->set('max_execution_time', $time);
180 2
        return $this;
181
    }
182
183
    /**
184
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Settings::getSettings() does not specify type hint for items of its traversable return value.
Loading history...
185
     */
186 35
    public function getSettings()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Settings::getSettings() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
187
    {
188 35
        return $this->settings;
189
    }
190
191
    /**
192
     * @param array $settings_array
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Settings::apply() does not specify type hint for items of its traversable parameter $settings_array.
Loading history...
193
     * @return $this
194
     */
195 2
    public function apply($settings_array)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Settings::apply() does not have parameter type hint for its parameter $settings_array but it should be possible to add it based on @param annotation "array".
Loading history...
196
    {
197 2
        foreach ($settings_array as $key => $value) {
198 2
            $this->set($key, $value);
199
        }
200
201 2
        return $this;
202
    }
203
204
    /**
205
     * @param int|bool $flag
206
     */
207
    public function setReadOnlyUser($flag)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Settings::setReadOnlyUser() does not have void return type hint.
Loading history...
208
    {
209
        $this->_ReadOnlyUser = $flag;
210
    }
211
212
    /**
213
     * @return bool
214
     */
215 35
    public function isReadOnlyUser()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Settings::isReadOnlyUser() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
216
    {
217 35
        return $this->_ReadOnlyUser;
218
    }
219
220
    /**
221
     * @param string $name
222
     * @return mixed|null
223
     */
224 36
    public function getSetting($name)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Settings::getSetting() does not have parameter type hint for its parameter $name but it should be possible to add it based on @param annotation "string".
Loading history...
225
    {
226 36
        if (!isset($this->settings[$name])) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
227
            return null;
228
        }
229
230 36
        return $this->get($name);
231
    }
232
}
233