Completed
Push — master ( 25abe9...41762b )
by John
23s
created

AbstractCommand::parseResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
crap 2
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
     * Default to the TelnetClient's prompt
31
     *
32
     * @return string
33
     */
34 1
    public function getPrompt()
35
    {
36 1
        return null;
37
    }
38
39
    /**
40
     * @param TelnetResponseInterface $response
41
     *
42
     * @return \Graze\Dynamark3Client\Dynamark3ResponseInterface
43
     */
44 4
    public function parseResponse(TelnetResponseInterface $response)
45
    {
46 4
        $errorCode = null;
47 4
        $promptMatches = $response->getPromptMatches();
48 4
        $prompt = reset($promptMatches);
49 4
        if ($response->isError()) {
50
            // error prompt - ERROR nnn
51 2
            $errorCode = substr($prompt, 6);
52 2
        }
53
54 4
        return new Dynamark3Response(
55 4
            $prompt,
56
            $errorCode
57 4
        );
58
    }
59
}
60