Failed Conditions
Pull Request — master (#102)
by Šimon
04:10
created

Settings::getQueryableSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
introduced by
Missing declare(strict_types = 1).
Loading history...
2
3
namespace ClickHouseDB;
4
5
/**
6
 * @see https://clickhouse.yandex/docs/en/operations/settings/
7
 */
8
class Settings
9
{
10
    /** @var mixed[] */
11
    private $settings = [];
12
13
    /** @var bool */
14
    private $httpCompressionEnabled = false;
15
16
    /**
17
     * @return mixed|null
18
     */
19 3
    public function get(string $key)
20
    {
21 3
        if (! $this->isSet($key)) {
22
            return null;
23
        }
24
25 3
        return $this->settings[$key];
26
    }
27
28 4
    public function isSet(string $key) : bool
29
    {
30 4
        return isset($this->settings[$key]);
31
    }
32
33
    /**
34
     * @param mixed $value
35
     */
36 7
    public function set(string $key, $value) : void
37
    {
38 7
        $this->settings[$key] = $value;
39 7
    }
40
41
    /**
42
     * @return mixed|null
43
     */
44 63
    public function isHttpCompressionEnabled()
45
    {
46 63
        return $this->httpCompressionEnabled;
47
    }
48
49 63
    public function setHttpCompression(bool $enable) : self
50
    {
51 63
        $this->httpCompressionEnabled = $enable;
52
53 63
        return $this;
54
    }
55
56
    public function readonly(int $flag) : self
57
    {
58
        $this->set('readonly', $flag);
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param string $session_id
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
65
     * @return $this
66
     */
67 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...
68
    {
69 2
        $this->set('session_id', $session_id);
70
71 2
        return $this;
72
    }
73
74
    /**
75
     * @return mixed|bool
76
     */
77 63
    public function getSessionId()
78
    {
79 63
        if (empty($this->settings['session_id'])) {
80 63
            return false;
81
        }
82
83 2
        return $this->get('session_id');
84
    }
85
86
    /**
87
     * @return string|bool
88
     */
89 2
    public function makeSessionId()
90
    {
91 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...
92
93 2
        return $this->getSessionId();
94
    }
95
96
    /**
97
     * @param mixed[] $forcedSettings
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
98
     * @return mixed[]
99
     */
100 63
    public function getQueryableSettings(array $forcedSettings) : array
101
    {
102 63
        $settings = $this->settings;
103 63
        if (! empty($forcedSettings)) {
104 11
            $settings = $forcedSettings + $this->settings;
105
        }
106
107 63
        return $settings;
108
    }
109
110
    /**
111
     * @param array $settings_array
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
@param annotation of method \ClickHouseDB\Settings::apply() does not specify type hint for items of its traversable parameter $settings_array.
Loading history...
112
     * @return $this
113
     */
114 1
    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...
115
    {
116 1
        foreach ($settings_array as $key => $value) {
117 1
            $this->set($key, $value);
118
        }
119
120 1
        return $this;
121
    }
122
123
    /**
124
     * @param string $name
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
125
     * @return mixed|null
126
     */
127 1
    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...
128
    {
129 1
        if (! isset($this->settings[$name])) {
130
            return null;
131
        }
132
133 1
        return $this->get($name);
134
    }
135
}
136