Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
26 / 26
CRAP
100.00% covered (success)
100.00%
1 / 1
HttpRequestHelperTrait
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
26 / 26
34
100.00% covered (success)
100.00%
1 / 1
 getPsrRequest
n/a
0 / 0
n/a
0 / 0
0
 getHeaders
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 hasHeader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHeader
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getPostParameters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasPostParameter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPostParameter
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getGetParameters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasGetParameter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getGetParameter
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getAttributeParameters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasAttributeParameter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAttributeParameter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCookieParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasCookie
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCookie
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getRequestMethod
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRequestGet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRequestPost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRequestPut
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRequestDelete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRequestHead
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isXmlHttpRequest
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getUserAgent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getQueryString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRequestURI
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getContentType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2019 (original work) Open Assessment Technologies SA;
19 *
20 */
21
22namespace oat\tao\model\http;
23
24use Psr\Http\Message\ServerRequestInterface;
25
26/**
27 * Trait HttpRequestHelperTrait
28 * @package oat\tao\model\http
29 * @author Moyon Camille
30 */
31trait HttpRequestHelperTrait
32{
33    /**
34     * Get the HTTP request
35     *
36     * @return ServerRequestInterface
37     */
38    abstract protected function getPsrRequest();
39
40    /**
41     * Retrieves all message header values.
42     *
43     * The keys represent the header name as it will be sent over the wire, and
44     * each value is an array of strings associated with the header.
45     *
46     *     // Represent the headers as a string
47     *     foreach ($message->getHeaders() as $name => $values) {
48     *         echo $name . ": " . implode(", ", $values);
49     *     }
50     *
51     *     // Emit headers iteratively:
52     *     foreach ($message->getHeaders() as $name => $values) {
53     *         foreach ($values as $value) {
54     *             header(sprintf('%s: %s', $name, $value), false);
55     *         }
56     *     }
57     *
58     * While header names are not case-sensitive, getHeaders() will preserve the
59     * exact case in which headers were originally specified.
60     *
61     * @return string[][] Returns an associative array of the message's headers. Each
62     *     key MUST be a header name, and each value MUST be an array of strings
63     *     for that header.
64     */
65    protected function getHeaders()
66    {
67        $headers = [];
68        foreach ($this->getPsrRequest()->getHeaders() as $name => $values) {
69            $headers[strtolower($name)] = (count($values) == 1) ? reset($values) : $values;
70        }
71        return $headers;
72    }
73
74    /**
75     * Checks if a header exists by the given case-insensitive name.
76     *
77     * @param string $name Case-insensitive header field name.
78     * @return bool Returns true if any header names match the given header
79     *     name using a case-insensitive string comparison. Returns false if
80     *     no matching header name is found in the message.
81     */
82    protected function hasHeader($name)
83    {
84        return $this->getPsrRequest()->hasHeader(strtolower($name));
85    }
86
87    /**
88     * Retrieves a message header value by the given case-insensitive name.
89     *
90     * This method returns an array of all the header values of the given
91     * case-insensitive header name.
92     *
93     * If the header does not appear in the message, this method MUST return an
94     * empty array.
95     *
96     * @param string $name Case-insensitive header field name.
97     * @param mixed $default Default value to return if the header does not exist.
98     * @return string[] An array of string values as provided for the given
99     *    header. If the header does not appear in the message, this method MUST
100     *    return an empty array.
101     */
102    protected function getHeader($name, $default = null)
103    {
104        if ($this->hasHeader($name)) {
105            return $this->getPsrRequest()->getHeader($name);
106        } else {
107            return $default;
108        }
109    }
110
111    /**
112     * Retrieve any parameters provided in the request body.
113     *
114     * If the request Content-Type is either application/x-www-form-urlencoded
115     * or multipart/form-data, and the request method is POST, this method MUST
116     * return the contents of $_POST.
117     *
118     * Otherwise, this method may return any results of deserializing
119     * the request body content; as parsing returns structured content, the
120     * potential types MUST be arrays or objects only. A null value indicates
121     * the absence of body content.
122     *
123     * @return null|array|object The deserialized body parameters, if any.
124     *     These will typically be an array or object.
125     */
126    protected function getPostParameters()
127    {
128        return (array) $this->getPsrRequest()->getParsedBody();
129    }
130
131    /**
132     * Check if the HTTP request contains POST parameter $name
133     *
134     * @param $name
135     * @return bool
136     */
137    protected function hasPostParameter($name)
138    {
139        return array_key_exists($name, $this->getPostParameters());
140    }
141
142    /**
143     * Get the POST parameter $name
144     *
145     * @param $name
146     * @param mixed $default Default value to return if the post parameter does not exist.
147     * @return mixed
148     */
149    protected function getPostParameter($name, $default = null)
150    {
151        if ($this->hasPostParameter($name)) {
152            return $this->getPostParameters()[$name];
153        }
154        return $default;
155    }
156
157    /**
158     * Retrieve query string arguments.
159     *
160     * Retrieves the deserialized query string arguments, if any.
161     *
162     * Note: the query params might not be in sync with the URI or server
163     * params. If you need to ensure you are only getting the original
164     * values, you may need to parse the query string from `getUri()->getQuery()`
165     * or from the `QUERY_STRING` server param.
166     *
167     * @return array
168     */
169    protected function getGetParameters()
170    {
171        return (array) $this->getPsrRequest()->getQueryParams();
172    }
173
174    /**
175     * Check if the HTTP request contains GET parameter $name
176     *
177     * @param $name
178     * @return bool
179     */
180    protected function hasGetParameter($name)
181    {
182        return array_key_exists($name, $this->getGetParameters());
183    }
184
185    /**
186     * Get the GET parameter $name
187     *
188     * @param $name
189     * @param mixed $default Default value to return if the get parameter does not exist.
190     * @return bool|mixed Return the GET parameter or false if any
191     */
192    protected function getGetParameter($name, $default = null)
193    {
194        if ($this->hasGetParameter($name)) {
195            return $this->getGetParameters()[$name];
196        }
197        return $default;
198    }
199
200    /**
201     * Retrieve attributes derived from the request.
202     *
203     * The request "attributes" may be used to allow injection of any
204     * parameters derived from the request: e.g., the results of path
205     * match operations; the results of decrypting cookies; the results of
206     * deserializing non-form-encoded message bodies; etc. Attributes
207     * will be application and request specific, and CAN be mutable.
208     *
209     * @return array Attributes derived from the request.
210     */
211    protected function getAttributeParameters()
212    {
213        return (array) $this->getPsrRequest()->getAttributes();
214    }
215
216    /**
217     * Check if the HTTP request contains attribute $name
218     *
219     * @param $name
220     * @return bool
221     */
222    protected function hasAttributeParameter($name)
223    {
224        return array_key_exists($name, $this->getAttributeParameters());
225    }
226
227    /**
228     * Retrieve a single derived request attribute.
229     *
230     * Retrieves a single derived request attribute as described in
231     * getAttributes(). If the attribute has not been previously set, returns
232     * the default value as provided.
233     *
234     * This method obviates the need for a hasAttribute() method, as it allows
235     * specifying a default value to return if the attribute is not found.
236     *
237     * @see getAttributes()
238     * @param string $name The attribute name.
239     * @param mixed $default Default value to return if the attribute does not exist.
240     * @return mixed
241     */
242    protected function getAttributeParameter($name, $default = null)
243    {
244        return $this->getPsrRequest()->getAttribute($name, $default);
245    }
246
247    /**
248     * Retrieve cookies.
249     *
250     * Retrieves cookies sent by the client to the server.
251     *
252     * The data MUST be compatible with the structure of the $_COOKIE superglobal.
253     *
254     * @return array
255     */
256    protected function getCookieParams()
257    {
258        return (array) $this->getPsrRequest()->getCookieParams();
259    }
260
261    /**
262     * Check if the given $name exists as a cookie
263     *
264     * @param $name
265     * @return bool
266     */
267    protected function hasCookie($name)
268    {
269        return array_key_exists($name, $this->getCookieParams());
270    }
271
272    /**
273     * Get the cookie associated to $name
274     *
275     * @param $name
276     * @param mixed $default Default value to return if the cookie does not exist.
277     * @return mixed
278     */
279    protected function getCookie($name, $default = null)
280    {
281        if ($this->hasCookie($name)) {
282            return $this->getCookieParams()[$name];
283        } else {
284            return $default;
285        }
286    }
287
288    /**
289     * Get the HTTP request method
290     *
291     * @return string
292     */
293    protected function getRequestMethod()
294    {
295        return $this->getPsrRequest()->getMethod();
296    }
297
298    /**
299     * Check if the HTTP request method is GET
300     *
301     * @return bool
302     */
303    protected function isRequestGet()
304    {
305        return $this->getRequestMethod() == 'GET';
306    }
307
308    /**
309     * Check if the HTTP request method is POST
310     *
311     * @return bool
312     */
313    protected function isRequestPost()
314    {
315        return $this->getRequestMethod() == 'POST';
316    }
317
318    /**
319     * Check if the HTTP request method is PUT
320     *
321     * @return bool
322     */
323    protected function isRequestPut()
324    {
325        return $this->getRequestMethod() == 'PUT';
326    }
327
328    /**
329     * Check if the HTTP request method is DELETE
330     *
331     * @return bool
332     */
333    protected function isRequestDelete()
334    {
335        return $this->getRequestMethod() == 'DELETE';
336    }
337
338    /**
339     * Check if the HTTP request method is HEAD
340     *
341     * @return bool
342     */
343    protected function isRequestHead()
344    {
345        return $this->getRequestMethod() == 'HEAD';
346    }
347
348    /**
349     * Check if the current request is using AJAX
350     *
351     * @return bool
352     */
353    protected function isXmlHttpRequest()
354    {
355        $serverParams = $this->getPsrRequest()->getServerParams();
356        if (isset($serverParams['HTTP_X_REQUESTED_WITH'])) {
357            if (strtolower($serverParams['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
358                return true;
359            }
360        }
361        return false;
362    }
363
364    /**
365     * Get the user agent from HTTP request header "user-agent"
366     *
367     * @return string[]
368     */
369    protected function getUserAgent()
370    {
371        return $this->getPsrRequest()->getHeader('user-agent');
372    }
373
374    /**
375     * Get the query string of HTTP request Uri
376     *
377     * @return string
378     */
379    protected function getQueryString()
380    {
381        return $this->getPsrRequest()->getUri()->getQuery();
382    }
383
384    /**
385     * Get the request uri e.q. the HTTP request path
386     *
387     * @return string
388     */
389    protected function getRequestURI()
390    {
391        return $this->getPsrRequest()->getUri()->getPath();
392    }
393
394    /**
395     * Get the content type from HTTP request header "content-type"
396     *
397     * @return string[]
398     */
399    protected function getContentType()
400    {
401        return $this->getPsrRequest()->getHeader('content-type');
402    }
403}