1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of graze/dynamark3-client. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @license https://github.com/graze/dynamark3-client/blob/master/LICENSE |
12
|
|
|
* @link https://github.com/graze/dynamark3-client |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Graze\Dynamark3Client\Command; |
16
|
|
|
|
17
|
|
|
use \Graze\Dynamark3Client\Command\CommandInterface; |
18
|
|
|
use \Graze\TelnetClient\TelnetResponseInterface; |
19
|
|
|
use \Graze\Dynamark3Client\Dynamark3Response; |
20
|
|
|
use \Graze\Dynamark3Client\Dynamark3Constants; |
21
|
|
|
|
22
|
|
|
abstract class AbstractCommand implements CommandInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
abstract public function getCommandText(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $arguments |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
2 |
|
public function getArgumentText(array $arguments) |
35
|
|
|
{ |
36
|
2 |
|
if (empty($arguments)) { |
37
|
|
|
return ''; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// add quotes to arguments |
41
|
2 |
|
array_walk( |
42
|
2 |
|
$arguments, |
43
|
2 |
|
function (&$value) { |
44
|
2 |
|
$value = sprintf('"%s"', $value); |
45
|
2 |
|
} |
46
|
|
|
); |
47
|
|
|
|
48
|
2 |
|
return ' ' . implode(' ', $arguments); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Default to the TelnetClient's prompt |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
2 |
|
public function getPrompt() |
57
|
|
|
{ |
58
|
2 |
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param TelnetResponseInterface $response |
63
|
|
|
* |
64
|
|
|
* @return \Graze\Dynamark3Client\Dynamark3ResponseInterface |
65
|
|
|
*/ |
66
|
6 |
View Code Duplication |
public function parseResponse(TelnetResponseInterface $response) |
|
|
|
|
67
|
|
|
{ |
68
|
6 |
|
$errorCode = null; |
69
|
6 |
|
$promptMatches = $response->getPromptMatches(); |
70
|
6 |
|
$prompt = reset($promptMatches); |
71
|
6 |
|
if ($response->isError()) { |
72
|
|
|
// error prompt - ERROR nnn |
73
|
3 |
|
$errorCode = substr($prompt, 6); |
74
|
|
|
} |
75
|
|
|
|
76
|
6 |
|
return new Dynamark3Response( |
77
|
6 |
|
$prompt, |
78
|
6 |
|
$errorCode |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.