Completed
Pull Request — master (#399)
by Anton
04:35
created

Response::redirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
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\Application\Exception\RedirectException;
14
use Bluz\Common\Exception\ComponentException;
15
use Bluz\Controller\Controller;
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 switchType($type)
82
 * @see      Instance::switchType()
83
 *
84
 * @method   static void  send()
85
 * @see      Instance::send()
86
 */
87
class Response
88
{
89
    use ProxyTrait;
90
91
    /**
92
     * Init instance
93
     *
94
     * @throws ComponentException
95
     */
96
    protected static function initInstance()
97
    {
98
        throw new ComponentException("Class `Proxy\\Request` required external initialization");
99
    }
100
101
    /**
102
     * Redirect to URL
103
     *
104
     * @param  string $url
105
     * @return void
106
     * @throws RedirectException
107
     */
108
    public static function redirect($url)
109
    {
110
        $redirect = new RedirectException();
111
        $redirect->setUrl($url);
112
        throw $redirect;
113
    }
114
115
    /**
116
     * Redirect to controller
117
     *
118
     * @param  string      $module
119
     * @param  string      $controller
120
     * @param  array       $params
121
     * @return void
122
     */
123
    public static function redirectTo($module = 'index', $controller = 'index', $params = [])
124
    {
125
        $url = Router::getUrl($module, $controller, $params);
126
        self::redirect($url);
127
    }
128
129
    /**
130
     * Reload current page please, be careful to avoid loop of reload
131
     *
132
     * @return void
133
     * @throws RedirectException
134
     */
135
    public static function reload()
136
    {
137
        self::redirect(Request::getUri());
138
    }
139
}
140