Total Complexity | 45 |
Total Lines | 426 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Request extends Message |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * The command to be executed. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | private $_command; |
||
46 | |||
47 | /** |
||
48 | * A query for the command. |
||
49 | * |
||
50 | * @var Query|null |
||
51 | */ |
||
52 | private $_query; |
||
53 | |||
54 | /** |
||
55 | * Creates a request to send to RouterOS. |
||
56 | * |
||
57 | * @param string $command The command to send. |
||
58 | * Can also contain arguments expressed in a shell-like syntax. |
||
59 | * @param Query|null $query A query to associate with the request. |
||
60 | * @param string|null $tag The tag for the request. |
||
61 | * |
||
62 | * @see setCommand() |
||
63 | * @see setArgument() |
||
64 | * @see setTag() |
||
65 | * @see setQuery() |
||
66 | */ |
||
67 | public function __construct($command, Query $query = null, $tag = null) |
||
68 | { |
||
69 | if (false !== strpos($command, '=') |
||
70 | && false !== ($spaceBeforeEquals = strrpos( |
||
71 | strstr($command, '=', true), |
||
72 | ' ' |
||
73 | )) |
||
74 | ) { |
||
75 | $this->parseArgumentString(substr($command, $spaceBeforeEquals)); |
||
76 | $command = rtrim(substr($command, 0, $spaceBeforeEquals)); |
||
77 | } |
||
78 | $this->setCommand($command); |
||
79 | $this->setQuery($query); |
||
80 | $this->setTag($tag); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * A shorthand gateway. |
||
85 | * |
||
86 | * This is a magic PHP method that allows you to call the object as a |
||
87 | * function. Depending on the argument given, one of the other functions in |
||
88 | * the class is invoked and its returned value is returned by this function. |
||
89 | * |
||
90 | * @param Query|Communicator|string|null $arg A {@link Query} to associate |
||
91 | * the request with, a {@link Communicator} to send the request over, |
||
92 | * an argument to get the value of, or NULL to get the tag. If a |
||
93 | * second argument is provided, this becomes the name of the argument to |
||
94 | * set the value of, and the second argument is the value to set. |
||
95 | * |
||
96 | * @return string|resource|int|$this Whatever the long form |
||
97 | * function returns. |
||
98 | */ |
||
99 | public function __invoke($arg = null) |
||
100 | { |
||
101 | if (func_num_args() > 1) { |
||
102 | return $this->setArgument(func_get_arg(0), func_get_arg(1)); |
||
103 | } |
||
104 | if ($arg instanceof Query) { |
||
105 | return $this->setQuery($arg); |
||
106 | } |
||
107 | if ($arg instanceof Communicator) { |
||
108 | return $this->send($arg); |
||
109 | } |
||
110 | return parent::__invoke($arg); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Sets the command to send to RouterOS. |
||
115 | * |
||
116 | * Sets the command to send to RouterOS. The command can use the API or CLI |
||
117 | * syntax of RouterOS, but either way, it must be absolute (begin with a |
||
118 | * "/") and without arguments. |
||
119 | * |
||
120 | * @param string $command The command to send. |
||
121 | * |
||
122 | * @return $this The request object. |
||
123 | * |
||
124 | * @see getCommand() |
||
125 | * @see setArgument() |
||
126 | */ |
||
127 | public function setCommand($command) |
||
128 | { |
||
129 | $command = (string) $command; |
||
130 | if (strpos($command, '/') !== 0) { |
||
131 | throw new InvalidArgumentException( |
||
132 | 'Commands must be absolute.', |
||
133 | InvalidArgumentException::CODE_ABSOLUTE_REQUIRED |
||
134 | ); |
||
135 | } |
||
136 | if (substr_count($command, '/') === 1) { |
||
137 | //Command line syntax convertion |
||
138 | $cmdParts = preg_split('#[\s/]+#sm', $command); |
||
139 | $cmdRes = array($cmdParts[0]); |
||
140 | for ($i = 1, $n = count($cmdParts); $i < $n; $i++) { |
||
141 | if ('..' === $cmdParts[$i]) { |
||
142 | $delIndex = count($cmdRes) - 1; |
||
143 | if ($delIndex < 1) { |
||
144 | throw new InvalidArgumentException( |
||
145 | 'Unable to resolve command', |
||
146 | InvalidArgumentException::CODE_CMD_UNRESOLVABLE |
||
147 | ); |
||
148 | } |
||
149 | unset($cmdRes[$delIndex]); |
||
150 | $cmdRes = array_values($cmdRes); |
||
151 | } else { |
||
152 | $cmdRes[] = $cmdParts[$i]; |
||
153 | } |
||
154 | } |
||
155 | $command = implode('/', $cmdRes); |
||
156 | } |
||
157 | if (!preg_match('#^/\S+$#sm', $command)) { |
||
158 | throw new InvalidArgumentException( |
||
159 | 'Invalid command supplied.', |
||
160 | InvalidArgumentException::CODE_CMD_INVALID |
||
161 | ); |
||
162 | } |
||
163 | $this->_command = $command; |
||
164 | return $this; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Gets the command that will be send to RouterOS. |
||
169 | * |
||
170 | * Gets the command that will be send to RouterOS in its API syntax. |
||
171 | * |
||
172 | * @return string The command to send. |
||
173 | * |
||
174 | * @see setCommand() |
||
175 | */ |
||
176 | public function getCommand() |
||
177 | { |
||
178 | return $this->_command; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Sets the query to send with the command. |
||
183 | * |
||
184 | * @param Query|null $query The query to be set. |
||
185 | * Setting NULL will remove the currently associated query. |
||
186 | * |
||
187 | * @return $this The request object. |
||
188 | * |
||
189 | * @see getQuery() |
||
190 | */ |
||
191 | public function setQuery(Query $query = null) |
||
192 | { |
||
193 | $this->_query = $query; |
||
194 | return $this; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Gets the currently associated query |
||
199 | * |
||
200 | * @return Query|null The currently associated query. |
||
201 | * |
||
202 | * @see setQuery() |
||
203 | */ |
||
204 | public function getQuery() |
||
205 | { |
||
206 | return $this->_query; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Sets the tag to associate the request with. |
||
211 | * |
||
212 | * Sets the tag to associate the request with. Setting NULL erases the |
||
213 | * currently set tag. |
||
214 | * |
||
215 | * @param string|null $tag The tag to set. |
||
216 | * |
||
217 | * @return $this The request object. |
||
218 | * |
||
219 | * @see getTag() |
||
220 | */ |
||
221 | public function setTag($tag) |
||
222 | { |
||
223 | return parent::setTag($tag); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Sets an argument for the request. |
||
228 | * |
||
229 | * @param string $name Name of the argument. |
||
230 | * @param string|resource|null $value Value of the argument as a string or |
||
231 | * seekable stream. |
||
232 | * Setting the value to NULL removes an argument of this name. |
||
233 | * If a seekable stream is provided, it is sent from its current |
||
234 | * position to its end, and the pointer is seeked back to its current |
||
235 | * position after sending. |
||
236 | * Non seekable streams, as well as all other types, are casted to a |
||
237 | * string. |
||
238 | * |
||
239 | * @return $this The request object. |
||
240 | * |
||
241 | * @see getArgument() |
||
242 | */ |
||
243 | public function setArgument($name, $value = '') |
||
244 | { |
||
245 | return parent::setAttribute($name, $value); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Gets the value of an argument. |
||
250 | * |
||
251 | * @param string $name The name of the argument. |
||
252 | * |
||
253 | * @return string|resource|null The value of the specified argument. |
||
254 | * Returns NULL if such an argument is not set. |
||
255 | * |
||
256 | * @see setAttribute() |
||
257 | */ |
||
258 | public function getArgument($name) |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Removes all arguments from the request. |
||
265 | * |
||
266 | * @return $this The request object. |
||
267 | */ |
||
268 | public function removeAllArguments() |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Get actionable debug info. |
||
275 | * |
||
276 | * This is a magic method available to PHP 5.6 and above, due to which |
||
277 | * output of var_dump() will be more actionable. |
||
278 | * |
||
279 | * You can still call it in earlier versions to get the object as a |
||
280 | * plain array. |
||
281 | * |
||
282 | * @return array The info, as an associative array. |
||
283 | */ |
||
284 | public function __debugInfo() |
||
285 | { |
||
286 | return parent::__debugInfo() + array( |
||
287 | 'command' => $this->_command, |
||
288 | 'query' => $this->_query |
||
289 | ); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Sends a request over a communicator. |
||
294 | * |
||
295 | * @param Communicator $com The communicator to send the request over. |
||
296 | * @param Registry|null $reg An optional registry to sync the request with. |
||
297 | * |
||
298 | * @return int The number of bytes sent. |
||
299 | * |
||
300 | * @see Client::sendSync() |
||
301 | * @see Client::sendAsync() |
||
302 | */ |
||
303 | public function send(Communicator $com, Registry $reg = null) |
||
304 | { |
||
305 | if (null !== $reg |
||
306 | && (null != $this->getTag() || !$reg->isTaglessModeOwner()) |
||
|
|||
307 | ) { |
||
308 | $originalTag = $this->getTag(); |
||
309 | $this->setTag($reg->getOwnershipTag() . $originalTag); |
||
310 | $bytes = $this->send($com); |
||
311 | $this->setTag($originalTag); |
||
312 | return $bytes; |
||
313 | } |
||
314 | if ($com->getTransmitter()->isPersistent()) { |
||
315 | $old = $com->getTransmitter()->lock(T\Stream::DIRECTION_SEND); |
||
316 | $bytes = $this->_send($com); |
||
317 | $com->getTransmitter()->lock($old, true); |
||
318 | return $bytes; |
||
319 | } |
||
320 | return $this->_send($com); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Sends a request over a communicator. |
||
325 | * |
||
326 | * The only difference with the non private equivalent is that this one does |
||
327 | * not do locking. |
||
328 | * |
||
329 | * @param Communicator $com The communicator to send the request over. |
||
330 | * |
||
331 | * @return int The number of bytes sent. |
||
332 | * |
||
333 | * @see Client::sendSync() |
||
334 | * @see Client::sendAsync() |
||
335 | */ |
||
336 | private function _send(Communicator $com) |
||
337 | { |
||
338 | if (!$com->getTransmitter()->isAcceptingData()) { |
||
339 | throw new SocketException( |
||
340 | 'Transmitter is invalid. Sending aborted.', |
||
341 | SocketException::CODE_REQUEST_SEND_FAIL |
||
342 | ); |
||
343 | } |
||
344 | $bytes = 0; |
||
345 | $bytes += $com->sendWord($this->getCommand()); |
||
346 | if (null !== ($tag = $this->getTag())) { |
||
347 | $bytes += $com->sendWord('.tag=' . $tag); |
||
348 | } |
||
349 | foreach ($this->attributes as $name => $value) { |
||
350 | $prefix = '=' . $name . '='; |
||
351 | $bytes += $com->sendWord($prefix, $value); |
||
352 | } |
||
353 | $query = $this->getQuery(); |
||
354 | if ($query instanceof Query) { |
||
355 | $bytes += $query->send($com); |
||
356 | } |
||
357 | $bytes += $com->sendWord(''); |
||
358 | return $bytes; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Verifies the request. |
||
363 | * |
||
364 | * Verifies the request against a communicator, i.e. whether the request |
||
365 | * could successfully be sent (assuming the connection is still opened). |
||
366 | * |
||
367 | * @param Communicator $com The Communicator to check against. |
||
368 | * |
||
369 | * @return $this The request object itself. |
||
370 | * |
||
371 | * @throws LengthException If the resulting length of an API word is not |
||
372 | * supported. |
||
373 | */ |
||
374 | public function verify(Communicator $com) |
||
375 | { |
||
376 | $com::verifyLengthSupport(strlen($this->getCommand())); |
||
377 | $com::verifyLengthSupport(strlen('.tag=' . (string)$this->getTag())); |
||
378 | foreach ($this->attributes as $name => $value) { |
||
379 | if (is_string($value)) { |
||
380 | $com::verifyLengthSupport(strlen('=' . $name . '=' . $value)); |
||
381 | } else { |
||
382 | $com::verifyLengthSupport( |
||
383 | strlen('=' . $name . '=') + |
||
384 | $com::seekableStreamLength($value) |
||
385 | ); |
||
386 | } |
||
387 | } |
||
388 | $query = $this->getQuery(); |
||
389 | if ($query instanceof Query) { |
||
390 | $query->verify($com); |
||
391 | } |
||
392 | return $this; |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Parses the arguments of a command. |
||
397 | * |
||
398 | * @param string $string The argument string to parse. |
||
399 | * |
||
400 | * @return void |
||
401 | */ |
||
402 | protected function parseArgumentString($string) |
||
463 | } |
||
464 | } |
||
465 | } |
||
466 |