Completed
Pull Request — master (#363)
by Anton
05:32
created

Response::redirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 1
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
/**
10
 * @namespace
11
 */
12
namespace Bluz\Proxy;
13
14
use Bluz\Application\Exception\RedirectException;
15
use Bluz\Application\Exception\ReloadException;
16
use Bluz\Common\Exception\ComponentException;
17
use Bluz\Controller\Controller;
18
use Bluz\Response\Response as Instance;
19
20
/**
21
 * Proxy to Response
22
 *
23
 * Example of usage
24
 * <code>
25
 *     use Bluz\Proxy\Response;
26
 *
27
 *     Response::setStatusCode(304);
28
 *     Response::setHeader('Location', '/index/index');
29
 * </code>
30
 *
31
 * @package  Bluz\Proxy
32
 * @author   Anton Shevchuk
33
 *
34
 * @method   static Instance getInstance()
35
 *
36
 * @method   static string getProtocolVersion()
37
 * @see      Bluz\Response\Response::getProtocolVersion()
38
 *
39
 * @method   static string getStatusCode()
40
 * @see      Bluz\Response\Response::getStatusCode()
41
 * @method   static void  setStatusCode($code)
42
 * @see      Bluz\Response\Response::setStatusCode()
43
 *
44
 * @method   static void  setReasonPhrase($phrase)
45
 * @see      Bluz\Response\Response::setReasonPhrase()
46
 * @method   static string getReasonPhrase()
47
 * @see      Bluz\Response\Response::getReasonPhrase()
48
 *
49
 * @method   static string getHeader($header)
50
 * @see      Bluz\Response\Response::getHeader()
51
 * @method   static array  getHeaderAsArray($header)
52
 * @see      Bluz\Response\Response::getHeaderAsArray()
53
 * @method   static bool   hasHeader($header)
54
 * @see      Bluz\Response\Response::hasHeader()
55
 * @method   static void   setHeader($header, $value)
56
 * @see      Bluz\Response\Response::setHeader()
57
 * @method   static void   addHeader($header, $value)
58
 * @see      Bluz\Response\Response::addHeader()
59
 * @method   static void   removeHeader($header)
60
 * @see      Bluz\Response\Response::removeHeader()
61
 *
62
 * @method   static array  getHeaders()
63
 * @see      Bluz\Response\Response::getHeaders()
64
 * @method   static void   setHeaders(array $headers)
65
 * @see      Bluz\Response\Response::setHeaders()
66
 * @method   static void   addHeaders(array $headers)
67
 * @see      Bluz\Response\Response::addHeaders()
68
 * @method   static void   removeHeaders()
69
 * @see      Bluz\Response\Response::removeHeaders()
70
 *
71
 * @method   static void  setBody($phrase)
72
 * @see      Bluz\Response\Response::setBody()
73
 * @method   static Controller  getBody()
74
 * @see      Bluz\Response\Response::getBody()
75
 * @method   static void  clearBody()
76
 * @see      Bluz\Response\Response::clearBody()
77
 *
78
 * @method   static void setCookie($name, $value = '', $expire = 0, $path = '/', $domain = '', $s = null, $h = null)
79
 * @see      Bluz\Response\Response::setCookie()
80
 * @method   static array getCookie()
81
 * @see      Bluz\Response\Response::getCookie()
82
 *
83
 * @method   static switchType($type)
84
 * @see      Bluz\Response\Response::switchType()
85
 *
86
 * @method   static void  send()
87
 * @see      Bluz\Response\Response::send()
88
 */
89
class Response extends AbstractProxy
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 3
    public static function redirect($url)
109
    {
110 3
        $redirect = new RedirectException();
111 3
        $redirect->setUrl($url);
112 3
        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 1
    public static function redirectTo($module = 'index', $controller = 'index', $params = array())
124
    {
125 1
        $url = Router::getUrl($module, $controller, $params);
126 1
        self::redirect($url);
127
    }
128
129
    /**
130
     * Reload current page please, be careful to avoid loop of reload
131
     *
132
     * @return void
133
     * @throws ReloadException
134
     */
135 1
    public static function reload()
136
    {
137 1
        self::redirect(Request::getUri());
138
    }
139
}
140