Passed
Push — master ( 6588d8...6bef90 )
by Alex
01:21
created

IncomingIlluminateRequest::getQueryParameters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 3
eloc 11
nc 3
nop 0
1
<?php
2
3
namespace POData\OperationContext\Web\Illuminate;
4
5
use Illuminate\Http\Request;
6
use POData\OperationContext\HTTPRequestMethod;
7
use POData\OperationContext\IHTTPRequest;
8
9
class IncomingIlluminateRequest implements IHTTPRequest
10
{
11
    /**
12
     * The Illuminate request.
13
     *
14
     * @var Request
15
     */
16
    private $request;
17
18
    /**
19
     * The request headers.
20
     *
21
     * @var array
22
     */
23
    private $headers = [];
24
25
    /**
26
     * The incoming url in raw format.
27
     *
28
     * @var string
29
     */
30
    private $rawUrl = null;
31
32
    /**
33
     * The request method (GET, POST, PUT, DELETE or MERGE).
34
     *
35
     * @var HTTPRequestMethod HttpVerb
36
     */
37
    private $method;
38
39
    /**
40
     * The query options as key value.
41
     *
42
     * @var array(string, string);
43
     */
44
    private $queryOptions = [];
45
46
    /**
47
     * A collection that represents mapping between query
48
     * option and its count.
49
     *
50
     * @var array(string, int)
51
     */
52
    private $queryOptionsCount = [];
53
54
    /**
55
     * IncomingIlluminateRequest constructor.
56
     *
57
     * @param Request $request
58
     */
59 View Code Duplication
    public function __construct(Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61
        $this->request = $request;
62
        $this->headers = [];
63
        $this->queryOptions = [];
64
        $this->queryOptionsCount = [];
65
        $this->method = new HTTPRequestMethod($this->request->getMethod());
66
    }
67
68
    /**
69
     * @return string RequestURI called by User with the value of QueryString
70
     */
71
    public function getRawUrl()
72
    {
73
        $this->rawUrl = $this->request->fullUrl();
74
75
        return $this->rawUrl;
76
    }
77
78
    /**
79
     * @param string $key The header name
80
     *
81
     * @return array|null|string
82
     */
83
    public function getRequestHeader($key)
84
    {
85
        $result = $this->request->header($key);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->request->header($key); of type string|array adds the type array to the return on line 91 which is incompatible with the return type declared by the interface POData\OperationContext\...quest::getRequestHeader of type string|null.
Loading history...
86
        //Zend returns false for a missing header...POData needs a null
87
        if (false === $result || '' === $result) {
88
            return null;
89
        }
90
91
        return $result;
92
    }
93
94
    /**
95
     * Returns the Query String Parameters (QSPs) as an array of KEY-VALUE pairs.  If a QSP appears twice
96
     * it will have two entries in this array.
97
     *
98
     * @return array
99
     */
100
    public function getQueryParameters()
101
    {
102
        //TODO: the contract is more specific than this, it requires the name and values to be decoded
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...
103
        //not sure how to test that...
104
        //Have to convert to the stranger format known to POData that deals with multiple query strings.
105
        //this makes this request a bit non compliant as it doesn't expose duplicate keys, something POData will
106
        //check for instead whatever parameter was last in the query string is set.  IE
107
        //odata.svc/?$format=xml&$format=json the format will be json
108
        $this->queryOptions = [];
109
        $this->queryOptionsCount = [];
110
111
        foreach ($this->request->all() as $key => $value) {
112
            $keyBitz = explode(';', $key);
113
            $newKey = strtolower($keyBitz[count($keyBitz) - 1]);
114
            $this->queryOptions[] = [$newKey => $value];
115
            if (!array_key_exists($key, $this->queryOptionsCount)) {
116
                $this->queryOptionsCount[$newKey] = 0;
117
            }
118
            $this->queryOptionsCount[$newKey]++;
119
        }
120
121
        return $this->queryOptions;
122
    }
123
124
    /**
125
     * @return HTTPRequestMethod
126
     */
127
    public function getMethod()
128
    {
129
        return $this->method;
130
    }
131
132
    /**
133
     * @return array|mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use resource|string|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
134
     */
135
    public function getAllInput()
136
    {
137
        $content = $this->request->all();
138
        return !empty($content) ? $content : $this->request->getContent();
139
    }
140
}
141