Connection::setPort()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
4
namespace Manticoresearch;
5
6
use Manticoresearch\Exceptions\RuntimeException;
7
use Psr\Log\LoggerInterface;
8
9
/**
10
 * Class Connection
11
 * @package Manticoresearch
12
 */
13
class Connection
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $config = array(
19
        'transport' => 'Http',
20
        'host' => '127.0.0.1',
21
        'scheme' => 'http',
22
        'path' => '',
23
        'port' => '9308',
24
        'timeout' => 300,
25
        'connect_timeout' => 0,
26
        'proxy' => null,
27
        'username' => null,
28
        'password' => null,
29
        'headers' => [],
30
        'curl' => [],
31
        'persistent' => true
32
    );
33
    /**
34
     * @var bool
35
     */
36
    protected $alive = true;
37
/*
38
 * $params['transport']  = transport class name
39
 * $params['host']       = hostname
40
 * $params['path']       = path
41
 * $params['port']       = port number
42
 * $params['timeout']    = connection timeout
43
 * $params['connect_timeout'] = connection connect timeout
44
 * $params['proxy']       = proxy host:port string
45
 * $params['username']  = username for http auth
46
 * $params['password']  = password for http auth
47
 * $params['headers']   = array of custom headers
48
 * $params['curl']      = array of pairs of curl option=>value
49
 * $params['persistent'] = bool if connection is persistent
50
 */
51
    /**
52
     * Connection constructor.
53
     * @param array $params
54
     */
55
    public function __construct(array $params)
56
    {
57
        $this->config = array_merge($this->config, $params);
58
    }
59
60
    /**
61
     * @param string $host
62
     * @return $this
63
     */
64
    public function setHost($host): self
65
    {
66
        $this->config['host'] = $host;
67
        return $this;
68
    }
69
70
    /**
71
     * @return mixed
72
     */
73
    public function getHost()
74
    {
75
        return $this->config['host'];
76
    }
77
78
    /**
79
     * @param string $path
80
     * @return $this
81
     */
82
    public function setPath(string $path): self
83
    {
84
        $this->config['path'] = $path;
85
        return $this;
86
    }
87
88
    /**
89
     * @return mixed
90
     */
91
    public function getPath()
92
    {
93
        return $this->config['path'];
94
    }
95
96
    /**
97
     * @param string|integer $port
98
     * @return $this
99
     */
100
    public function setPort($port): self
101
    {
102
        $this->config['port'] = (int)$port;
103
        return $this;
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109
    public function getPort()
110
    {
111
        return $this->config['port'];
112
    }
113
114
    /**
115
     * @param integer $timeout
116
     * @return $this
117
     */
118
    public function setTimeout($timeout): self
119
    {
120
        $this->config['timeout'] = (int)$timeout;
121
        return $this;
122
    }
123
124
    /**
125
     * @return mixed
126
     */
127
    public function getHeaders()
128
    {
129
        return $this->config['headers'];
130
    }
131
132
    /**
133
     * @param array $headers
134
     * @return $this
135
     */
136
    public function setheaders($headers): self
137
    {
138
        $this->config['headers'] = $headers;
139
        return $this;
140
    }
141
142
    /**
143
     * @return mixed
144
     */
145
    public function getTimeout()
146
    {
147
        return $this->config['timeout'];
148
    }
149
150
    /**
151
     * @param integer $connect_timeout
152
     * @return $this
153
     */
154
    public function setConnectTimeout($connect_timeout): self
155
    {
156
        $this->config['connect_timeout'] = (int)$connect_timeout;
157
        return $this;
158
    }
159
160
    /**
161
     * @return mixed
162
     */
163
    public function getConnectTimeout()
164
    {
165
        return $this->config['connect_timeout'];
166
    }
167
168
    /**
169
     * @param Transport $transport
170
     * @return $this
171
     */
172
    public function setTransport($transport): self
173
    {
174
        $this->config['transport'] = $transport;
175
        return $this;
176
    }
177
178
    /**
179
     * @return mixed
180
     */
181
    public function getTransport()
182
    {
183
        return $this->config['transport'];
184
    }
185
186
    /**
187
     * @param LoggerInterface $logger
188
     * @return mixed
189
     * @throws \Exception
190
     */
191
    public function getTransportHandler(LoggerInterface $logger)
192
    {
193
        return Transport::create($this->getTransport(), $this, $logger);
194
    }
195
196
    /**
197
     * @param array $config
198
     * @return $this
199
     */
200
    public function setConfig($config): self
201
    {
202
        foreach ($config as $ckey => $cvalue) {
203
            $this->config[$ckey] = $cvalue;
204
        }
205
        return $this;
206
    }
207
208
    /**
209
     * @param string|null $key
210
     * @return mixed|null
211
     *
212
     */
213
    public function getConfig($key = null)
214
    {
215
        if ($key === null) {
216
            return $this->config;
217
        }
218
        return $this->config[$key] ?? null;
219
    }
220
221
    /**
222
     * @param array|Connection $params|self
223
     * @return Connection
224
     */
225
    public static function create($params)
226
    {
227
        if (is_array($params)) {
228
            return new self($params);
229
        }
230
        if ($params instanceof self) {
231
            return $params;
232
        }
233
        throw new RuntimeException('connection must receive array of parameters or self');
234
    }
235
236
    /**
237
     * @return bool
238
     */
239
    public function isAlive(): bool
240
    {
241
        return $this->alive;
242
    }
243
244
    /**
245
     * @param bool $state
246
     */
247
    public function mark(bool $state)
248
    {
249
        $this->alive = $state;
250
    }
251
}
252