ResponseUrlTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 4
eloc 12
c 5
b 0
f 1
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRedirectResponse() 0 8 2
A getRedirectUrlResponse() 0 6 1
A getReloadResponse() 0 4 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