Total Complexity | 40 |
Total Lines | 347 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like Client often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Client |
||
26 | { |
||
27 | /** |
||
28 | * |
||
29 | */ |
||
30 | const VERSION = '1.0.0'; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $config = []; |
||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $connectionStrategy = StaticRoundRobin::class; |
||
40 | /** |
||
41 | * @var ConnectionPool |
||
42 | */ |
||
43 | protected $connectionPool; |
||
44 | |||
45 | /** |
||
46 | * @var LoggerInterface|NullLogger |
||
47 | */ |
||
48 | protected $logger; |
||
49 | |||
50 | protected $lastResponse; |
||
51 | |||
52 | /* |
||
53 | * $config can be a connection array or |
||
54 | * $config['connections] = array of connections |
||
55 | * $config['connectionStrategy'] = class name of pool strategy |
||
56 | */ |
||
57 | public function __construct($config = [], LoggerInterface $logger = null) |
||
58 | { |
||
59 | $this->setConfig($config); |
||
60 | $this->logger = $logger ?? new NullLogger(); |
||
61 | $this->initConnections(); |
||
62 | } |
||
63 | |||
64 | protected function initConnections() |
||
65 | { |
||
66 | $connections = []; |
||
67 | if (isset($this->config['connections'])) { |
||
68 | foreach ($this->config['connections'] as $connection) { |
||
69 | if (is_array($connection)) { |
||
70 | $connections[] = Connection::create($connection); |
||
71 | } else { |
||
72 | $connections[] = $connection; |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | |||
77 | if (empty($connections)) { |
||
78 | $connections[] = Connection::create($this->config); |
||
79 | } |
||
80 | if (isset($this->config['connectionStrategy'])) { |
||
81 | if (is_string($this->config['connectionStrategy'])) { |
||
82 | $strategyName = '\\Manticoresearch\\Connection\\Strategy\\' . $this->config['connectionStrategy']; |
||
83 | if (class_exists($strategyName)) { |
||
84 | $strategy = new $strategyName(); |
||
85 | } elseif (class_exists($this->config['connectionStrategy'])) { |
||
86 | $strategyName = $this->config['connectionStrategy']; |
||
87 | $strategy = new $strategyName(); |
||
88 | } |
||
89 | } elseif ($this->config['connectionStrategy'] instanceof SelectorInterface) { |
||
90 | $strategy = $this->config['connectionStrategy']; |
||
91 | } else { |
||
92 | throw new RuntimeException('Cannot create a strategy based on provided settings!'); |
||
93 | } |
||
94 | } else { |
||
95 | $strategy = new $this->connectionStrategy; |
||
96 | } |
||
97 | if (!isset($this->config['retries'])) { |
||
98 | $this->config['retries'] = count($connections); |
||
99 | } |
||
100 | $this->connectionPool = new Connection\ConnectionPool( |
||
101 | $connections, |
||
102 | $strategy ?? new $this->connectionStrategy, |
||
103 | $this->config['retries'] |
||
104 | ); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param string|array $hosts |
||
109 | */ |
||
110 | public function setHosts($hosts) |
||
111 | { |
||
112 | $this->config['connections'] = $hosts; |
||
113 | $this->initConnections(); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param array $config |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function setConfig(array $config): self |
||
121 | { |
||
122 | $this->config = array_merge($this->config, $config); |
||
123 | return $this; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param array $config |
||
128 | * @return Client |
||
129 | */ |
||
130 | public static function create($config): Client |
||
131 | { |
||
132 | return self::createFromArray($config); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param array $config |
||
137 | * @return Client |
||
138 | */ |
||
139 | public static function createFromArray($config): Client |
||
140 | { |
||
141 | return new self($config); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return mixed |
||
146 | */ |
||
147 | public function getConnections() |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @return ConnectionPool |
||
154 | */ |
||
155 | public function getConnectionPool(): ConnectionPool |
||
156 | { |
||
157 | return $this->connectionPool; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Endpoint: search |
||
162 | * @param array $params |
||
163 | * @param bool $obj |
||
164 | * @return array|Response |
||
165 | */ |
||
166 | public function search(array $params = [], $obj = false) |
||
167 | { |
||
168 | $endpoint = new Endpoints\Search($params); |
||
169 | $response = $this->request($endpoint); |
||
170 | if ($obj === true) { |
||
171 | return $response; |
||
172 | } else { |
||
173 | return $response->getResponse(); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Endpoint: insert |
||
179 | * @param array $params |
||
180 | * @return array |
||
181 | */ |
||
182 | public function insert(array $params = []) |
||
183 | { |
||
184 | $endpoint = new Endpoints\Insert($params); |
||
185 | $response = $this->request($endpoint); |
||
186 | |||
187 | return $response->getResponse(); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Endpoint: replace |
||
192 | * @param array $params |
||
193 | * @return mixed |
||
194 | */ |
||
195 | public function replace(array $params = []) |
||
196 | { |
||
197 | $endpoint = new Endpoints\Replace($params); |
||
198 | $response = $this->request($endpoint); |
||
199 | |||
200 | return $response->getResponse(); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Endpoint: update |
||
205 | * @param array $params |
||
206 | * @return array |
||
207 | */ |
||
208 | public function update(array $params = []) |
||
209 | { |
||
210 | $endpoint = new Endpoints\Update($params); |
||
211 | $response = $this->request($endpoint); |
||
212 | |||
213 | return $response->getResponse(); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Endpoint: sql |
||
218 | * @param array $params |
||
219 | * @return array |
||
220 | */ |
||
221 | public function sql(array $params = []) |
||
222 | { |
||
223 | $endpoint = new Endpoints\Sql($params); |
||
224 | if (isset($params['mode'])) { |
||
225 | $endpoint->setMode($params['mode']); |
||
226 | $response = $this->request($endpoint, ['responseClass' => 'Manticoresearch\\Response\\SqlToArray']); |
||
227 | } else { |
||
228 | $response = $this->request($endpoint); |
||
229 | } |
||
230 | return $response->getResponse(); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Endpoint: delete |
||
235 | * @param array $params |
||
236 | * @return array |
||
237 | */ |
||
238 | public function delete(array $params = []) |
||
239 | { |
||
240 | $endpoint = new Endpoints\Delete($params); |
||
241 | $response = $this->request($endpoint); |
||
242 | |||
243 | return $response->getResponse(); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Endpoint: pq |
||
248 | */ |
||
249 | public function pq(): Pq |
||
250 | { |
||
251 | return new Pq($this); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Endpoint: indices |
||
256 | */ |
||
257 | public function indices(): Indices |
||
258 | { |
||
259 | return new Indices($this); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Endpoint: nodes |
||
264 | */ |
||
265 | public function nodes(): Nodes |
||
268 | } |
||
269 | |||
270 | public function cluster(): Cluster |
||
271 | { |
||
272 | return new Cluster($this); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Return Index object |
||
277 | * |
||
278 | * @param string|null $name Name of index |
||
279 | * |
||
280 | * @return \Manticoresearch\Index |
||
281 | */ |
||
282 | public function index(string $name = null): Index |
||
283 | { |
||
284 | return new Index($this, $name); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Endpoint: bulk |
||
289 | * @param array $params |
||
290 | * @return array |
||
291 | */ |
||
292 | public function bulk(array $params = []) |
||
293 | { |
||
294 | $endpoint = new Endpoints\Bulk($params); |
||
295 | $response = $this->request($endpoint); |
||
296 | |||
297 | return $response->getResponse(); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Endpoint: suggest |
||
302 | * @param array $params |
||
303 | * @return array |
||
304 | */ |
||
305 | public function suggest(array $params = []) |
||
312 | } |
||
313 | |||
314 | public function keywords(array $params = []) |
||
315 | { |
||
316 | $endpoint = new Endpoints\Keywords(); |
||
317 | $endpoint->setIndex($params['index']); |
||
318 | $endpoint->setBody($params['body']); |
||
319 | $response = $this->request($endpoint, ['responseClass' => 'Manticoresearch\\Response\\SqlToArray']); |
||
320 | return $response->getResponse(); |
||
321 | } |
||
322 | |||
323 | public function explainQuery(array $params = []) |
||
330 | } |
||
331 | |||
332 | |||
333 | /* |
||
334 | * @return Response |
||
335 | */ |
||
336 | |||
337 | public function request(Request $request, array $params = []): Response |
||
362 | } |
||
363 | |||
364 | /* |
||
365 | * |
||
366 | * @return Response |
||
367 | */ |
||
368 | |||
369 | public function getLastResponse(): Response |
||
374 |