Passed
Push — master ( 07feba...51d451 )
by Charles
06:12
created

Request::getDecryptedBody()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace yrc\web;
4
5
use yii\base\InvalidConfigException;
6
use yii\web\Request as BaseRequest;
7
use yii\web\RequestParserInterface;
8
use yrc\web\Response;
9
use Yii;
10
11
class Request extends BaseRequest
12
{
13
    private $_bodyParams;
14
15
    private $_decryptedBody;
16
17
    public function getDecryptedBody()
18
    {
19
        if ($this->getContentType() !== 'application/vnd.25519+json') {
20
            return $this->getRawBody();
21
        }
22
23
        $this->getBodyParams();
24
        return $this->_decryptedBody;
25
    }
26
27
    /**
28
     * Returns the request parameters given in the request body.
29
     *
30
     * Request parameters are determined using the parsers configured in [[parsers]] property.
31
     * If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()`
32
     * to parse the [[rawBody|request body]].
33
     * @return array the request parameters given in the request body.
34
     * @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]].
35
     * @see getMethod()
36
     * @see getBodyParam()
37
     * @see setBodyParams()
38
     */
39
    public function getBodyParams()
40
    {
41
        if ($this->_bodyParams === null) {
42
            if (isset($_POST[$this->methodParam])) {
43
                $this->_bodyParams = $_POST;
44
                unset($this->_bodyParams[$this->methodParam]);
45
                return $this->_bodyParams;
46
            }
47
            $rawContentType = $this->getContentType();
48
            if (($pos = strpos($rawContentType, ';')) !== false) {
49
                // e.g. text/html; charset=UTF-8
50
                $contentType = substr($rawContentType, 0, $pos);
51
            } else {
52
                $contentType = $rawContentType;
53
            }
54
            if (isset($this->parsers[$contentType])) {
55
                $parser = Yii::createObject($this->parsers[$contentType]);
56
                if (!($parser instanceof RequestParserInterface)) {
57
                    throw new InvalidConfigException("The '$contentType' request parser is invalid. It must implement the yii\\web\\RequestParserInterface.");
58
                }
59
                $this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType);
60
                if ($rawContentType === 'application/vnd.25519+json') {
61
                    $this->_decryptedBody = $parser->getDecryptedBody();
0 ignored issues
show
Bug introduced by
The method getDecryptedBody() does not exist on yii\web\RequestParserInterface. It seems like you code against a sub-type of said class. However, the method does not exist in yii\web\JsonParser. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
                    /** @scrutinizer ignore-call */ 
62
                    $this->_decryptedBody = $parser->getDecryptedBody();
Loading history...
62
                }
63
            } elseif (isset($this->parsers['*'])) {
64
                $parser = Yii::createObject($this->parsers['*']);
65
                if (!($parser instanceof RequestParserInterface)) {
66
                    throw new InvalidConfigException('The fallback request parser is invalid. It must implement the yii\\web\\RequestParserInterface.');
67
                }
68
                $this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType);
69
            } elseif ($this->getMethod() === 'POST') {
70
                // PHP has already parsed the body so we have all params in $_POST
71
                $this->_bodyParams = $_POST;
72
            } else {
73
                $this->_bodyParams = [];
74
                mb_parse_str($this->getRawBody(), $this->_bodyParams);
75
            }
76
        }
77
        return $this->_bodyParams;
78
    }
79
}
80