Completed
Push — master ( 0bfbf7...50b91b )
by Jonathan
03:57
created

MessageHelper::paginationFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Minotaur
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2017 Appertly
19
 * @license   Apache-2.0
20
 */
21
namespace Minotaur\Http;
22
23
use Psr\Http\Message\ServerRequestInterface as Request;
24
use Psr\Http\Message\ResponseInterface as Response;
25
use Caridea\Auth\Principal;
26
use Caridea\Http\PaginationFactory;
27
28
/**
29
 * Controller trait with some handy methods.
30
 */
31
trait MessageHelper
32
{
33
    /**
34
     * Gets a `Map` of the request body content.
35
     *
36
     * @param $request - The request
37
     * @return array<string,mixed> The Map of request body content
38
     */
39 2
    protected function getParsedBodyMap(Request $request): \ConstMap
40
    {
41 2
        $body = $request->getParsedBody();
42 2
        return is_array($body) ? new \HH\Map($body) : new \HH\Map();
43
    }
44
45
    /**
46
     * Gets a `Map` of the request query params.
47
     *
48
     * @param $request - The request
49
     * @return array<string,mixed> The Map of query params
50
     */
51
    protected function getQueryParamsMap(Request $request): \ConstMap
52
    {
53
        return new \HH\Map($request->getQueryParams());
54
    }
55
56
    /**
57
     * Cleanly writes the body to the response.
58
     *
59
     * @param $response - The HTTP response
60
     * @param $body - The body to write
61
     * @return - The same or new response
0 ignored issues
show
Documentation introduced by
The doc-type - could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
62
     */
63 1
    protected function write(Response $response, $body): Response
64
    {
65 1
        $response->getBody()->write((string) $body);
66 1
        return $response;
67
    }
68
69
    /**
70
     * Checks the `If-Modified-Since` header, maybe sending 304 Not Modified.
71
     *
72
     * @param $request - The HTTP request
73
     * @param $response - The HTTP response
74
     * @param $timestamp - The timestamp for comparison
75
     * @return - The same or new response
0 ignored issues
show
Documentation introduced by
The doc-type - could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
76
     */
77 3
    protected function ifModSince(Request $request, Response $response, int $timestamp): Response
78
    {
79 3
        $ifModSince = $request->getHeaderLine('If-Modified-Since');
80 3
        if ($ifModSince && $timestamp <= strtotime($ifModSince)) {
81 1
            return $response->withStatus(304, "Not Modified");
82
        }
83 2
        return $response;
84
    }
85
86
    /**
87
     * Checks the `If-None-Match` header, maybe sending 304 Not Modified.
88
     *
89
     * @param $request - The HTTP request
90
     * @param $response - The HTTP response
91
     * @param $etag - The ETag for comparison
92
     * @return - The same or new response
0 ignored issues
show
Documentation introduced by
The doc-type - could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
93
     */
94 3
    protected function ifNoneMatch(Request $request, Response $response, string $etag): Response
95
    {
96 3
        $ifNoneMatch = $request->getHeaderLine('If-None-Match');
97 3
        if ($ifNoneMatch && $etag === $ifNoneMatch) {
98 1
            return $response->withStatus(304, "Not Modified");
99
        }
100 2
        return $response;
101
    }
102
103
    /**
104
     * Redirects the user to another URL.
105
     *
106
     * @param $response - The HTTP response
107
     * @return - The new response
0 ignored issues
show
Documentation introduced by
The doc-type - could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
108
     */
109 1
    protected function redirect(Response $response, int $code, string $url): Response
110
    {
111 1
        return $response->withStatus($code)->withHeader('Location', $url);
112
    }
113
114
    /**
115
     * Gets the stored principal, or the anonymous user if none was found.
116
     *
117
     * @param $request - The HTTP request
118
     * @return - The authenticated principal
0 ignored issues
show
Documentation introduced by
The doc-type - could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
119
     */
120
    protected function getPrincipal(Request $request): Principal
121
    {
122
        $principal = $request->getAttribute('principal', Principal::getAnonymous());
123
        if (!($principal instanceof Principal)) {
124
            throw new \UnexpectedValueException("Type mismatch: principal");
125
        }
126
        return $principal;
127
    }
128
129
    /**
130
     * Gets a pagination factory
131
     *
132
     * @return - The pagination factory
0 ignored issues
show
Documentation introduced by
The doc-type - could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
133
     */
134 1
    protected function paginationFactory(): PaginationFactory
135
    {
136 1
        return new PaginationFactory();
137
    }
138
}
139