Passed
Push — master ( a6375b...78beea )
by Anton
05:54
created

Response::redirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Proxy;
12
13
use Bluz\Common\Exception\ComponentException;
14
use Bluz\Controller\Controller;
15
use Bluz\Http\Exception\RedirectException;
16
use Bluz\Response\Response as Instance;
17
18
/**
19
 * Proxy to Response
20
 *
21
 * Example of usage
22
 * <code>
23
 *     use Bluz\Proxy\Response;
24
 *
25
 *     Response::setStatusCode(304);
26
 *     Response::setHeader('Location', '/index/index');
27
 * </code>
28
 *
29
 * @package  Bluz\Proxy
30
 * @author   Anton Shevchuk
31
 *
32
 * @method   static Instance getInstance()
33
 *
34
 * @method   static string getProtocolVersion()
35
 * @see      Instance::getProtocolVersion()
36
 *
37
 * @method   static string getStatusCode()
38
 * @see      Instance::getStatusCode()
39
 * @method   static void  setStatusCode($code)
40
 * @see      Instance::setStatusCode()
41
 *
42
 * @method   static void  setReasonPhrase($phrase)
43
 * @see      Instance::setReasonPhrase()
44
 * @method   static string getReasonPhrase()
45
 * @see      Instance::getReasonPhrase()
46
 *
47
 * @method   static string getHeader($header)
48
 * @see      Instance::getHeader()
49
 * @method   static array  getHeaderAsArray($header)
50
 * @see      Instance::getHeaderAsArray()
51
 * @method   static bool   hasHeader($header)
52
 * @see      Instance::hasHeader()
53
 * @method   static void   setHeader($header, $value)
54
 * @see      Instance::setHeader()
55
 * @method   static void   addHeader($header, $value)
56
 * @see      Instance::addHeader()
57
 * @method   static void   removeHeader($header)
58
 * @see      Instance::removeHeader()
59
 *
60
 * @method   static array  getHeaders()
61
 * @see      Instance::getHeaders()
62
 * @method   static void   setHeaders(array $headers)
63
 * @see      Instance::setHeaders()
64
 * @method   static void   addHeaders(array $headers)
65
 * @see      Instance::addHeaders()
66
 * @method   static void   removeHeaders()
67
 * @see      Instance::removeHeaders()
68
 *
69
 * @method   static void  setBody($phrase)
70
 * @see      Instance::setBody()
71
 * @method   static Controller  getBody()
72
 * @see      Instance::getBody()
73
 * @method   static void  clearBody()
74
 * @see      Instance::clearBody()
75
 *
76
 * @method   static void setCookie($name, $value = '', $expire = 0, $path = '/', $domain = '', $s = null, $h = null)
77
 * @see      Instance::setCookie()
78
 * @method   static array getCookie()
79
 * @see      Instance::getCookie()
80
 *
81
 * @method   static string getType()
82
 * @see      Instance::getType()
83
 * @method   static void setType($type)
84
 * @see      Instance::setType()
85
 *
86
 * @method   static void  send()
87
 * @see      Instance::send()
88
 */
89
final class Response
90
{
91
    use ProxyTrait;
92
93
    /**
94
     * Init instance
95
     *
96
     * @throws ComponentException
97
     */
98
    private static function initInstance()
0 ignored issues
show
Unused Code introduced by
The method initInstance() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
99
    {
100
        throw new ComponentException("Class `Proxy\\Request` required external initialization");
101
    }
102
103
    /**
104
     * Redirect to URL
105
     *
106
     * @param  string $url
107
     *
108
     * @return void
109
     * @throws RedirectException
110
     */
111 5
    public static function redirect($url): void
112
    {
113 5
        $redirect = new RedirectException();
114 5
        $redirect->setUrl($url);
115 5
        throw $redirect;
116
    }
117
118
    /**
119
     * Redirect to controller
120
     *
121
     * @param  string $module
122
     * @param  string $controller
123
     * @param  array  $params
124
     *
125
     * @return void
126
     * @throws RedirectException
127
     */
128 3
    public static function redirectTo($module = 'index', $controller = 'index', array $params = []): void
129
    {
130 3
        $url = Router::getFullUrl($module, $controller, $params);
131 3
        self::redirect($url);
132
    }
133
134
    /**
135
     * Reload current page please, be careful to avoid loop of reload
136
     *
137
     * @return void
138
     * @throws RedirectException
139
     */
140 1
    public static function reload(): void
141
    {
142 1
        self::redirect((string) Request::getUri());
143
    }
144
}
145