Passed
Push — master ( 8bf151...470498 )
by Radu
02:44
created

ResponseUrlTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 15
dl 0
loc 49
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRedirectResponse() 0 8 2
A getRedirectUrlResponse() 0 6 1
A forceRedirect() 0 5 1
A getReloadResponse() 0 4 1
1
<?php
2
namespace WebServCo\Framework\Traits;
3
4
trait ResponseUrlTrait
5
{
6
    abstract protected function request();
7
8
    /**
9
     * Redirect to an application location (Request target).
10
     * This method returns a HttpResponse object that needs to be in turn returned to the application.
11
     */
12
    final protected function getRedirectResponse($location, $addSuffix = true)
13
    {
14
        $url = $this->request()->getAppUrl();
15
        $url .= $location;
16
        if ($addSuffix) {
17
            $url .= $this->request()->getSuffix();
18
        }
19
        return $this->getRedirectUrlResponse($url);
20
    }
21
22
    /**
23
     * Redirect to an application location (Request target).
24
     * This method sends a HttpRequest, forcing a redirect.
25
    */
26
    final protected function forceRedirect($location, $addSuffix = true)
27
    {
28
        $response = $this->getRedirectResponse($location, $addSuffix);
29
        $response->send();
30
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
31
    }
32
33
    /**
34
     * Redirect to the current URL.
35
     * This method returns a HttpResponse object that needs to be in turn returned to the application.
36
     */
37
    final protected function getReloadResponse($removeParameters = [])
38
    {
39
        $url = $this->request()->getUrl($removeParameters);
40
        return $this->getRedirectUrlResponse($url);
41
    }
42
43
    /**
44
     * Redirect to a full URL.
45
     * This method returns a HttpResponse object that needs to be in turn returned to the application.
46
     */
47
    final protected function getRedirectUrlResponse($url)
48
    {
49
        return new \WebServCo\Framework\HttpResponse(
50
            null,
51
            302,
52
            ['Location' => $url]
53
        );
54
    }
55
}
56