InterpretAsCommand::interpret()   A
last analyzed

Complexity

Conditions 5
Paths 10

Size

Total Lines 25

Duplication

Lines 8
Ratio 32 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
dl 8
loc 25
ccs 14
cts 14
cp 1
rs 9.2088
c 0
b 0
f 0
cc 5
nc 10
nop 2
crap 5
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 Exception;
18
use Graze\TelnetClient\Exception\TelnetException;
19
use Graze\TelnetClient\Exception\TelnetExceptionInterface;
20
use Graze\TelnetClient\Exception\UndefinedCommandException;
21
use Socket\Raw\Socket;
22
23
class InterpretAsCommand
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $WILL;
29
30
    /**
31
     * @var string
32
     */
33
    protected $WONT;
34
35
    /**
36
     * @var string
37
     */
38
    protected $DO;
39
40
    /**
41
     * @var string
42
     */
43
    protected $DONT;
44
45
    /**
46
     * @var string
47
     */
48
    protected $IAC;
49
50 8
    public function __construct()
51
    {
52 8
        $this->WILL = chr(251);
53 8
        $this->WONT = chr(252);
54 8
        $this->DO = chr(253);
55 8
        $this->DONT = chr(254);
56 8
        $this->IAC = chr(255);
57 8
    }
58
59
    /**
60
     * @param string $character
61
     * @param Socket $socket
62
     *
63
     * @return bool
64
     * @throws TelnetExceptionInterface
65
     */
66 7
    public function interpret($character, Socket $socket)
67
    {
68 7
        if ($character != $this->IAC) {
69 1
            return false;
70
        }
71
72
        try {
73 6
            $command = $socket->read(1);
74 5
            $option = $socket->read(1);
75
76 5 View Code Duplication
            if (in_array($command, [$this->DO, $this->DONT])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
77 2
                $socket->write($this->IAC . $this->WONT . $option);
78 2
                return true;
79
            }
80
81 3 View Code Duplication
            if (in_array($command, [$this->WILL, $this->WONT])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
82 2
                $socket->write($this->IAC . $this->DONT . $option);
83 2
                return true;
84
            }
85 2
        } catch (Exception $e) {
86 1
            throw new TelnetException('failed negotiating IAC', 0, $e);
87
        }
88
89 1
        throw new UndefinedCommandException($command);
90
    }
91
}
92