MessageHelper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 4
dl 0
loc 82
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getParsedBodyArray() 0 5 2
A write() 0 5 1
A ifModSince() 0 8 3
A ifNoneMatch() 0 8 3
A redirect() 0 4 1
A paginationFactory() 0 4 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
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
 * @copyright 2017-2018 LibreWorks contributors
20
 * @license   Apache-2.0
21
 */
22
namespace Caridea\Http;
23
24
use Psr\Http\Message\ServerRequestInterface as Request;
25
use Psr\Http\Message\ResponseInterface as Response;
26
use Caridea\Http\PaginationFactory;
27
28
/**
29
 * Controller trait with some handy methods.
30
 */
31
trait MessageHelper
32
{
33
    /**
34
     * Gets an associative array of the request body content.
35
     *
36
     * @param \Psr\Http\Message\ServerRequestInterface $request  The request
37
     * @return array<string,mixed>  The associative array of request body content
38
     */
39 2
    protected function getParsedBodyArray(Request $request): array
40
    {
41 2
        $body = $request->getParsedBody();
42 2
        return is_array($body) ? $body : [];
43
    }
44
45
    /**
46
     * Cleanly writes the body to the response.
47
     *
48
     * @param \Psr\Http\Message\ResponseInterface $response  The HTTP response
49
     * @param mixed $body  The body to write
50
     * @return \Psr\Http\Message\ResponseInterface  The same or new response
51
     */
52 1
    protected function write(Response $response, $body): Response
53
    {
54 1
        $response->getBody()->write((string) $body);
55 1
        return $response;
56
    }
57
58
    /**
59
     * Checks the `If-Modified-Since` header, maybe sending 304 Not Modified.
60
     *
61
     * @param \Psr\Http\Message\ServerRequestInterface $request  The HTTP request
62
     * @param \Psr\Http\Message\ResponseInterface $response  The HTTP response
63
     * @param int $timestamp  The timestamp for comparison
64
     * @return \Psr\Http\Message\ResponseInterface  The same or new response
65
     */
66 3
    protected function ifModSince(Request $request, Response $response, int $timestamp): Response
67
    {
68 3
        $ifModSince = $request->getHeaderLine('If-Modified-Since');
69 3
        if ($ifModSince && $timestamp <= strtotime($ifModSince)) {
70 1
            return $response->withStatus(304, "Not Modified");
71
        }
72 2
        return $response;
73
    }
74
75
    /**
76
     * Checks the `If-None-Match` header, maybe sending 304 Not Modified.
77
     *
78
     * @param \Psr\Http\Message\ServerRequestInterface $request  The HTTP request
79
     * @param \Psr\Http\Message\ResponseInterface $response  The HTTP response
80
     * @param string $etag  The ETag for comparison
81
     * @return \Psr\Http\Message\ResponseInterface  The same or new response
82
     */
83 3
    protected function ifNoneMatch(Request $request, Response $response, string $etag): Response
84
    {
85 3
        $ifNoneMatch = $request->getHeaderLine('If-None-Match');
86 3
        if ($ifNoneMatch && $etag === $ifNoneMatch) {
87 1
            return $response->withStatus(304, "Not Modified");
88
        }
89 2
        return $response;
90
    }
91
92
    /**
93
     * Redirects the user to another URL.
94
     *
95
     * @param \Psr\Http\Message\ResponseInterface $response  The HTTP response
96
     * @return \Psr\Http\Message\ResponseInterface  The new response
97
     */
98 1
    protected function redirect(Response $response, int $code, string $url): Response
99
    {
100 1
        return $response->withStatus($code)->withHeader('Location', $url);
101
    }
102
103
    /**
104
     * Gets a pagination factory
105
     *
106
     * @return \Caridea\Http\PaginationFactory  The pagination factory
107
     */
108 1
    protected function paginationFactory(): PaginationFactory
109
    {
110 1
        return new PaginationFactory();
111
    }
112
}
113