Completed
Pull Request — master (#6)
by John
02:30
created

TelnetClient::factory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of graze/telnet-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/telnet-client/blob/master/LICENSE
12
 * @link https://github.com/graze/telnet-client
13
 */
14
15
namespace Graze\TelnetClient;
16
17
use \Graze\TelnetClient\TelnetClientInterface;
18
use \Graze\TelnetClient\PromptMatcher;
19
use \Graze\TelnetClient\InterpretAsCommand;
20
use \Socket\Raw\Socket;
21
use \Socket\Raw\Factory as SocketFactory;
22
use \Graze\TelnetClient\TelnetClientBuilder;
23
use \Exception;
24
use \Graze\TelnetClient\Exception\TelnetException;
25
26
class TelnetClient implements TelnetClientInterface
27
{
28
    /**
29
     * @var SocketFactory
30
     */
31
    protected $socketFactory;
32
33
    /**
34
     * @var PromptMatcher
35
     */
36
    protected $promptMatcher;
37
38
    /**
39
     * @var InterpretAsCommand
40
     */
41
    protected $interpretAsCommand;
42
43
    /**
44
     * @var string
45
     */
46
    protected $prompt = '\$';
47
48
    /**
49
     * @var string
50
     */
51
    protected $promptError = 'ERROR';
52
53
    /**
54
     * @var string
55
     */
56
    protected $lineEnding = "\n";
57
58
    /**
59
     * @var Socket
60
     */
61
    protected $socket;
62
63
    /**
64
     * @var string
65
     */
66
    protected $buffer;
67
68
    /**
69
     * @var string
70
     */
71
    protected $NULL;
72
73
    /**
74
     * @var string
75
     */
76
    protected $DC1;
77
78
    /**
79
     * @var string
80
     */
81
    protected $IAC;
82
83
    /**
84
     * @param SocketFactory $socketFactory
85
     * @param PromptMatcher $promptMatcher
86
     * @param InterpretAsCommand $interpretAsCommand
87
     */
88 10
    public function __construct(
89
        SocketFactory $socketFactory,
90
        PromptMatcher $promptMatcher,
91
        InterpretAsCommand $interpretAsCommand
92
    ) {
93 10
        $this->socketFactory = $socketFactory;
94 10
        $this->promptMatcher = $promptMatcher;
95 10
        $this->interpretAsCommand = $interpretAsCommand;
96
97 10
        $this->NULL = chr(0);
98 10
        $this->DC1 = chr(17);
99 10
    }
100
101
    /**
102
     * @param string $dsn
103
     * @param string $prompt
104
     * @param string $promptError
105
     * @param string $lineEnding
106
     */
107 9
    public function connect($dsn, $prompt = null, $promptError = null, $lineEnding = null)
108
    {
109 9
        if ($prompt !== null) {
110 2
            $this->setPrompt($prompt);
111 2
        }
112
113 9
        if ($promptError !== null) {
114 2
            $this->setPromptError($promptError);
115 2
        }
116
117 9
        if ($lineEnding !== null) {
118 8
            $this->setLineEnding($lineEnding);
119 8
        }
120
121
        try {
122 9
            $socket = $this->socketFactory->createClient($dsn);
123
        }
124 9
        catch (Exception $e) {
125
            throw new TelnetException(sprintf('unable to create socket connection to [%s]', $dsn), 0, $e);
126
        }
127
128 9
        $this->setSocket($socket);
129 9
    }
130
131
    /**
132
     * @param string $prompt
133
     */
134 1
    public function setPrompt($prompt)
135
    {
136 1
        $this->prompt = $prompt;
137 1
    }
138
139
    /**
140
     * @param string $promptError
141
     */
142 1
    public function setPromptError($promptError)
143
    {
144 1
        $this->promptError = $promptError;
145 1
    }
146
147
    /**
148
     * @param string $lineEnding
149
     */
150 7
    public function setLineEnding($lineEnding)
151
    {
152 7
        $this->lineEnding = $lineEnding;
153 7
    }
154
155
    /**
156
     * @param Socket $socket
157
     */
158 9
    public function setSocket(Socket $socket)
159
    {
160 9
        $this->socket = $socket;
161 9
    }
162
163
    /**
164
     * @param string $command
165
     * @param string $prompt
166
     *
167
     * @return \Graze\TelnetClient\TelnetResponseInterface
168
     */
169 10
    public function execute($command, $prompt = null)
170
    {
171 10
        if (!$this->socket) {
172 1
            throw new TelnetException('attempt to execute without a connection - call connect first');
173
        }
174
175 9
        $this->write($command);
176 8
        return $this->getResponse($prompt);
177
    }
178
179
    /**
180
     * @param string $command
181
     *
182
     * @return void
183
     */
184 9
    protected function write($command)
185
    {
186
        try {
187 9
            $this->socket->write($command . $this->lineEnding);
188
        }
189 9
        catch (Exception $e) {
190 1
            throw new TelnetException(sprintf('failed writing to socket [%s]', $command), 0, $e);
191
        }
192 8
    }
193
194
    /**
195
     * @param string $prompt
196
     *
197
     * @return \Graze\TelnetClient\TelnetResponseInterface
198
     */
199 8
    protected function getResponse($prompt = null)
200
    {
201 8
        $isError = false;
202 8
        $buffer = '';
203
        do {
204
            // process one character at a time
205
            try {
206 8
                $character = $this->socket->read(1);
207
            }
208 8
            catch (Exception $e) {
209 1
                throw new TelnetException('failed reading from socket', 0, $e);
210
            }
211
212 7
            if (in_array($character, [$this->NULL, $this->DC1])) {
213
                break;
214
            }
215
216 7
            if ($this->interpretAsCommand->interpret($character, $this->socket)) {
217
                continue;
218
            }
219
220 7
            $buffer .= $character;
221
222
            // check for prompt
223 7
            if ($this->promptMatcher->isMatch($prompt ?: $this->prompt, $buffer, $this->lineEnding)) {
224 4
                break;
225
            }
226
227
            // check for error prompt
228 7
            if ($this->promptMatcher->isMatch($this->promptError, $buffer, $this->lineEnding)) {
229 3
                $isError = true;
230 3
                break;
231
            }
232
233 7
        } while (true);
234
235 7
        return new TelnetResponse(
236 7
            $isError,
237 7
            $this->promptMatcher->getResponseText(),
238 7
            $this->promptMatcher->getMatches()
239 7
        );
240
    }
241
242
243
    /**
244
     * @return TelnetClientInterface
245
     */
246 1
    public static function factory()
247
    {
248 1
        return new static(
249 1
            new SocketFactory(),
250 1
            new PromptMatcher(),
251 1
            new InterpretAsCommand()
252 1
        );
253
    }
254
255 8
    public function __destruct()
256
    {
257 8
        if (!$this->socket) {
258 1
            return;
259
        }
260
261 7
        $this->socket->close();
262 7
    }
263
}
264