ResponseUrlTrait::getReloadResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Traits;
6
7
use WebServCo\Framework\Http\Response;
8
9
trait ResponseUrlTrait
10
{
11
    abstract protected function request(): \WebServCo\Framework\Interfaces\RequestInterface;
12
13
    /**
14
     * Redirect to an application location (Request target).
15
     * This method returns a Response object that needs to be in turn returned to the application.
16
     */
17
    final protected function getRedirectResponse(string $location, bool $addSuffix = true): Response
18
    {
19
        $url = $this->request()->getAppUrl();
20
        $url .= $location;
21
        if ($addSuffix) {
22
            $url .= $this->request()->getSuffix();
23
        }
24
        return $this->getRedirectUrlResponse($url);
25
    }
26
27
    /**
28
     * Redirect to the current URL.
29
     * This method returns a Response object that needs to be in turn returned to the application.
30
     *
31
     * @param array<int,string> $removeParameters
32
     */
33
    final protected function getReloadResponse(array $removeParameters = []): Response
34
    {
35
        $url = $this->request()->getUrl($removeParameters);
36
        return $this->getRedirectUrlResponse($url);
37
    }
38
39
    /**
40
     * Redirect to a full URL.
41
     * This method returns a Response object that needs to be in turn returned to the application.
42
     *
43
     * "303 See Other (since HTTP/1.1)
44
     * The response to the request can be found under another URI using the GET method.
45
     * When received in response to a POST (or PUT/DELETE), the client should presume that the server has received
46
     * the data and should issue a new GET request to the given URI."
47
     */
48
    final protected function getRedirectUrlResponse(string $url, int $statusCode = 303): Response
49
    {
50
        return new Response(
51
            '',
52
            $statusCode,
53
            ['Location' => [$url]],
54
        );
55
    }
56
}
57