Completed
Push — master ( f6c83d...982358 )
by Vasily
06:50
created

TelnetHoneypotConnection::onRead()   D

Complexity

Conditions 10
Paths 25

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
c 3
b 0
f 0
nc 25
nop 0
dl 0
loc 32
rs 4.8196

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace PHPDaemon\Examples;
3
4
/**
5
 * @package    NetworkServers
6
 * @subpackage TelnetHoneypot
7
 *
8
 * @author     Vasily Zorin <[email protected]>
9
 */
10
class TelnetHoneypot extends \PHPDaemon\Network\Server
11
{
12
    public $connectionClass = '\PHPDaemon\Examples\TelnetHoneypotConnection';
13
14
    /**
15
     * Setting default config options
16
     * Overriden from ConnectionPool::getConfigDefaults
17
     * @return array|false
18
     */
19
    protected function getConfigDefaults()
20
    {
21
        return [
22
            // @todo add description strings
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
23
            'listen' => '0.0.0.0',
24
            'port' => 23,
25
        ];
26
    }
27
}
28
29
class TelnetHoneypotConnection extends \PHPDaemon\Network\Connection
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
30
{
31
    /**
32
     * Called when new data received.
33
     * @return void
34
     */
35
    public function onRead()
36
    {
37
        while (!is_null($line = $this->readline())) {
38
            $finish =
39
                (mb_orig_strpos($line, $s = "\xff\xf4\xff\xfd\x06") !== false)
40
                || (mb_orig_strpos($line, $s = "\xff\xec") !== false)
41
                || (mb_orig_strpos($line, $s = "\x03") !== false)
42
                || (mb_orig_strpos($line, $s = "\x04") !== false);
43
44
            $e = explode(' ', rtrim($line, "\r\n"), 2);
45
46
            $cmd = trim($e[0]);
47
48
            if ($cmd === 'ping') {
49
                $this->writeln('pong');
50
            } elseif (
51
                ($cmd === 'exit')
52
                || ($cmd === 'quit')
53
            ) {
54
                $this->finish();
55
            } else {
56
                $this->writeln('Unknown command "' . $cmd . '"');
57
            }
58
59
            if (
60
                (mb_orig_strlen($line) > 1024)
61
                || $finish
62
            ) {
63
                $this->finish();
64
            }
65
        }
66
    }
67
68
    /**
69
     * Called when the session finished
70
     * @return void
71
     */
72
    public function onFinish()
73
    {
74
        $this->writeln('Bye!');
75
    }
76
}
77