GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( a445f1...b2ee95 )
by Jérémy
01:44
created

Request::parseData()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.6845
cc 4
eloc 13
nc 2
nop 0
1
<?php
2
3
namespace CapMousse\ReactRestify\Http;
4
5
use React\Http\Request as ReactHttpRequest;
6
use CapMousse\ReactRestify\Evenement\EventEmitter;
7
8
class Request extends EventEmitter
9
{
10
    /** @var \React\Http\Request */
11
    public $httpRequest;
12
13
    /** @var string */
14
    private $content;
15
16
    /** @var array */
17
    private $data = [];
18
19
    /**
20
     * @param ReactHttpRequest $httpRequest
21
     */
22
    public function __construct(ReactHttpRequest $httpRequest)
23
    {
24
        $this->httpRequest = $httpRequest;
25
    }
26
27
    /**
28
     * Set the raw data of the request
29
     * @param string $content
30
     */
31
    public function setContent($content)
32
    {
33
        $this->content = $content;
34
    }
35
36
    /**
37
     * Set the raw data of the request
38
     * @return string
39
     */
40
    public function getContent()
41
    {
42
        return $this->content;
43
    }
44
45
    /**
46
     * Get formated headers
47
     * @return array
48
     */
49
    public function getHeaders()
50
    {
51
        $headers = array_change_key_case($this->httpRequest->getHeaders(), CASE_LOWER);
52
        $headers = array_map(function ($value) {
53
            return strtolower($value);
54
        }, $headers);
55
56
        return $headers;
57
    }
58
59
    /**
60
     * Parse request data
61
     * @return void
62
     */
63
    public function parseData()
64
    {
65
        $headers = $this->getHeaders();
66
67
        if (!in_array($this->httpRequest->getMethod(), ['PUT', 'POST'])) {
68
            return $this->emit('end');
69
        }
70
71
        $this->httpRequest->on('data', function($data) use ($headers, &$dataResult) {
72
            $dataResult .= $data;
73
74
            if (isset($headers["Content-Length"])) {
75
                if (strlen($dataResult) == $headers["Content-Length"]) {
76
                    $this->httpRequest->close();
77
                }
78
            } else {
79
                $this->httpRequest->close();
80
            }
81
        });
82
83
        $this->httpRequest->on('end', function() use (&$dataResult) {
84
            $this->onEnd($dataResult);
85
        });
86
    }
87
88
    /**
89
     * On request end
90
     * @param  string $dataResult
91
     * @return void
92
     */
93
    private function onEnd($dataResult)
94
    {
95
        if ($dataResult === null) return $this->emit('end');
96
97
        if ($this->isJson()) $this->parseJson($dataResult);
98
        else $this->parseStr($dataResult);
99
    }
100
101
    /**
102
     * Parse querystring
103
     * @param  string $dataString
104
     * @return void
105
     */
106
    private function parseStr($dataString)
107
    {
108
        $data = [];
109
        parse_str($dataString, $data);
110
111
        $this->setContent($dataString);
112
        if(is_array($data)) $this->setData($data);
113
114
        $this->emit('end');
115
    }
116
117
    /**
118
     * Parse json string
119
     * @param  string $jsonString
120
     * @return void
121
     */
122
    private function parseJson($jsonString)
123
    {
124
        $jsonData = json_decode($jsonString, true);
125
126
        if ($jsonData === null) {
127
            $this->emit('error', [json_last_error_msg()]);
128
            return;
129
        }
130
131
        $this->setContent($jsonString);
132
        $this->setData($jsonData);
133
        $this->emit('end');
134
    }
135
136
    /**
137
     * Check if current request is a json request
138
     * @return boolean
139
     */
140
    public function isJson()
141
    {
142
        $headers = $this->getHeaders();
143
144
        return isset($headers['content-type']) && $headers['content-type'] == 'application/json';
145
    }
146
147
    /**
148
     * Set the data array
149
     * @param array $data array of data
150
     */
151
    public function setData($data)
152
    {
153
        $this->data = array_merge($data, $this->data);
154
    }
155
156
    /**
157
     * Get the data array
158
     * @return array
159
     */
160
    public function getData()
161
    {
162
        return $this->data;
163
    }
164
165
    public function __get($name)
166
    {
167
        return isset($this->data[$name]) ? $this->data[$name] : false;
168
    }
169
170
    public function __set($name, $value)
171
    {
172
        $this->data[$name] = $value;
173
    }
174
}
175