JsonpSend   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 42 11
1
<?php
2
namespace PHPDaemon\SockJS\Methods;
3
4
/**
5
 * @package    Libraries
6
 * @subpackage SockJS
7
 * @author     Vasily Zorin <[email protected]>
8
 */
9
class JsonpSend extends Generic
10
{
11
    protected $contentType = 'text/plain';
12
    protected $allowedMethods = 'POST';
13
14
    /**
15
     * Called when request iterated
16
     * @return void
17
     */
18
    public function run()
19
    {
20
        if ($this->stage++ > 0) {
21
            $this->header('500 Too Busy');
22
            return;
23
        }
24
        $this->noncache();
25
        if (isset($_SERVER['HTTP_CONTENT_TYPE']) && $_SERVER['HTTP_CONTENT_TYPE'] === 'application/x-www-form-urlencoded') {
26
            if (!isset($_POST['d']) || !is_string($_POST['d']) || !mb_orig_strlen($_POST['d'])) {
27
                $this->header('500 Internal Server Error');
28
                echo 'Payload expected.';
29
                return;
30
            }
31
            if (!json_decode($_POST['d'], true)) {
32
                $this->header('500 Internal Server Error');
33
                echo 'Broken JSON encoding.';
34
                return;
35
            }
36
            $payload = $_POST['d'];
37
        } else {
38
            if ($this->attrs->raw === '') {
39
                $this->header('500 Internal Server Error');
40
                echo 'Payload expected.';
41
                return;
42
            }
43
            if (!json_decode($this->attrs->raw, true)) {
44
                $this->header('500 Internal Server Error');
45
                echo 'Broken JSON encoding.';
46
                return;
47
            }
48
            $payload = $this->attrs->raw;
49
        }
50
        $this->appInstance->publish('c2s:' . $this->sessId, $payload, function ($redis) {
0 ignored issues
show
Documentation Bug introduced by
The method publish does not exist on object<PHPDaemon\Core\AppInstance>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
51
            if ($redis->result === 0) {
52
                $this->header('404 Not Found');
53
            } else {
54
                echo 'ok';
55
            }
56
            $this->finish();
57
        });
58
        $this->sleep(30);
59
    }
60
}
61