Completed
Push — master ( 34af06...2d7359 )
by Vasily
05:38 queued 01:26
created

Request::init()   D

Complexity

Conditions 9
Paths 4

Size

Total Lines 42
Code Lines 32

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 9
eloc 32
nc 4
nop 0
dl 0
loc 42
rs 4.909
1
<?php
2
namespace PHPDaemon\Applications\GibsonREST;
3
4
class Request extends \PHPDaemon\HTTPRequest\Generic
5
{
6
7
    protected $result;
8
    protected $cmd;
9
    protected $args;
10
    protected $performed = false;
11
12
    /*
13
     * Constructor.
14
     * @return void
15
     */
16
    public function init()
17
    {
18
        try {
19
            $this->header('Content-Type: text/plain');
20
            //$this->header('Content-Type: application/x-json');
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
21
        } catch (\Exception $e) {
22
        }
23
        if (!$this->importCmdArgs()) {
24
            return;
25
        }
26
        $this->sleep(5, true); // setting timeout 5 seconds */
27
        $this->onSessionStart(function () {
0 ignored issues
show
Bug introduced by
The method onSessionStart() does not exist on PHPDaemon\Applications\GibsonREST\Request. Did you maybe mean onSessionStartEvent()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
28
            $this->wakeup();
29
            if ($this->cmd === 'LOGIN') {
30
                if (sizeof($this->args) !== 2) {
31
                    $this->result = ['$err' => 'You must pass exactly 2 arguments.'];
32
                    $this->wakeup();
33
                    return;
34
                }
35
                if ((hash_equals($this->appInstance->config->username->value,
36
                    $this->args[0]) + hash_equals($this->appInstance->config->password->value,
37
                        $this->args[1])) < 2) {
38
                    $this->result = ['$err' => 'Wrong username and/or password.'];
39
                    return;
40
                }
41
                $this->attrs->session['logged'] = $this->appInstance->config->credver;
42
                $this->result = ['$ok' => 1];
43
                $this->wakeup();
44
                return;
45
            } elseif ($this->cmd === 'LOGOUT') {
46
                unset($this->attrs->session['logged']);
47
                $this->result = ['$ok' => 1];
48
                $this->wakeup();
49
                return;
50
            }
51
            if (!isset($this->attrs->session['logged']) || $this->attrs->session['logged'] < $this->appInstance->config->credver) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
52
                $this->result = ['$err' => 'You must be authenticated.'];
53
                $this->wakeup();
54
                return;
55
            }
56
        });
57
    }
58
59
    /*
60
     * Import command name and arguments from input
61
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
62
     */
63
    protected function importCmdArgs()
64
    {
65
        $this->cmd = static::getString($_GET['cmd']);
66
        if ($this->cmd === '') {
67
            $e = explode('/',
68
                isset($this->attrs->server['SUBPATH']) ? $this->attrs->server['SUBPATH'] : $this->attrs->server['DOCUMENT_URI']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
69
            $this->cmd = array_shift($e);
70
            $this->args = sizeof($e) ? array_map('urldecode', $e) : [];
71
        }
72
        if (!$this->appInstance->gibson->isCommand($this->cmd) && $this->cmd !== 'LOGIN' && $this->cmd !== 'LOGOUT') {
0 ignored issues
show
Bug introduced by
The property gibson does not seem to exist in PHPDaemon\Core\AppInstance.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
73
            $this->result = ['$err' => 'Unrecognized command'];
74
            return false;
75
        }
76
        return true;
77
    }
78
79
    /*
80
     * Import command name and arguments from input
81
     * @return void
82
     */
83
84
    /**
85
     * Called when request iterated.
86
     * @return integer Status.
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
87
     */
88
    public function run()
89
    {
90
        if (!$this->performed && $this->result === null) {
91
            $this->performed = true;
92
            $this->importCmdArgsFromPost();
93
            $this->performCommand();
94
            if ($this->result === null) {
95
                $this->sleep(5);
96
                return;
97
            }
98
        }
99
        echo json_encode($this->result);
100
    }
101
102
    /*
103
     * Performs command
104
     * @return void
105
     */
106
107
    protected function importCmdArgsFromPost()
108
    {
109
        if ($this->result === null) {
110
            foreach (static::getArray($_POST['args']) as $arg) {
111
                if (is_string($arg)) {
112
                    $this->args[] = $arg;
113
                }
114
            }
115
        }
116
    }
117
118
    protected function performCommand()
119
    {
120
        $args = $this->args;
121
        $args[] = function ($conn) {
122
            if (!$conn->isFinal()) {
123
                return;
124
            }
125
            $this->result = $conn->result;
126
            $this->wakeup();
127
        };
128
        $func = [$this->appInstance->gibson, $this->cmd];
0 ignored issues
show
Bug introduced by
The property gibson does not seem to exist in PHPDaemon\Core\AppInstance.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
129
        $func(...$args);
130
    }
131
}
132