Passed
Push — master ( a04e7d...b9fea2 )
by Iman
03:04
created

Responder::weThrowNew()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\HeyMan;
4
5
use Illuminate\Auth\Access\AuthorizationException;
6
use Symfony\Component\HttpFoundation\Response;
7
8
class Responder
9
{
10
    private $response;
11
12
    private $exception;
13
14
    /**
15
     * Create a new redirect response to the given path.
16
     *
17
     * @param  string  $path
18
     * @param  int  $status
19
     * @param  array  $headers
20
     * @param  bool|null  $secure
21
     */
22
    public function redirectTo($path, $status = 302, $headers = [], $secure = null)
23
    {
24
        $this->response = response()->redirectTo($path, $status, $headers, $secure);
25
    }
26
27
28
    /**
29
     * Return a new response from the application.
30
     *
31
     * @param  string  $content
32
     * @param  int  $status
33
     * @param  array  $headers
34
     */
35
    public function make($content = '', $status = 200, array $headers = [])
36
    {
37
        $this->response = response()->make($content, $status, $headers);
38
    }
39
40
    /**
41
     * Return a new view response from the application.
42
     *
43
     * @param  string  $view
44
     * @param  array  $data
45
     * @param  int  $status
46
     * @param  array  $headers
47
     */
48
    public function view($view, $data = [], $status = 200, array $headers = [])
49
    {
50
        $this->response = response()->view($view, $data, $status, $headers );
51
    }
52
53
    /**
54
     * Return a new JSON response from the application.
55
     *
56
     * @param  string|array  $data
57
     * @param  int  $status
58
     * @param  array  $headers
59
     * @param  int  $options
60
     */
61
    public function json($data = [], $status = 200, array $headers = [], $options = 0)
62
    {
63
        $this->response = response()->json($data, $status, $headers, $options);
64
    }
65
    /**
66
     * Return a new JSONP response from the application.
67
     *
68
     * @param  string  $callback
69
     * @param  string|array  $data
70
     * @param  int  $status
71
     * @param  array  $headers
72
     * @param  int  $options
73
     */
74
    public function jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0)
75
    {
76
        $this->response = response()->jsonp($callback, $data, $status, $headers, $options);
77
    }
78
79
    /**
80
     * Return a new streamed response from the application.
81
     *
82
     * @param  \Closure  $callback
83
     * @param  int  $status
84
     * @param  array  $headers
85
     */
86
    public function stream($callback, $status = 200, array $headers = [])
87
    {
88
        $this->response = response()->stream($callback, $status, $headers);
89
    }
90
91
    /**
92
     * Return a new streamed response as a file download from the application.
93
     *
94
     * @param  \Closure  $callback
95
     * @param  string|null  $name
96
     * @param  array  $headers
97
     * @param  string|null  $disposition
98
     */
99
    public function streamDownload($callback, $name = null, array $headers = [], $disposition = 'attachment')
100
    {
101
        $this->response = response()->streamDownload($callback, $name, $headers, $disposition );
102
    }
103
104
    /**
105
     * Create a new file download response.
106
     *
107
     * @param  \SplFileInfo|string  $file
108
     * @param  string|null  $name
109
     * @param  array  $headers
110
     * @param  string|null  $disposition
111
     */
112
    public function download($file, $name = null, array $headers = [], $disposition = 'attachment')
113
    {
114
        $this->response = response()->download($file, $name, $headers, $disposition );
115
    }
116
117
    /**
118
     * Create a new redirect response to a named route.
119
     *
120
     * @param  string  $route
121
     * @param  array  $parameters
122
     * @param  int  $status
123
     * @param  array  $headers
124
     */
125
    public function redirectToRoute($route, $parameters = [], $status = 302, $headers = [])
126
    {
127
        $this->response = response()->redirectToRoute($route, $parameters, $status, $headers);
128
    }
129
130
    /**
131
     * Create a new redirect response to a controller action.
132
     *
133
     * @param  string  $action
134
     * @param  array  $parameters
135
     * @param  int  $status
136
     * @param  array  $headers
137
     */
138
    public function redirectToAction($action, $parameters = [], $status = 302, $headers = [])
139
    {
140
        $this->response = response()->redirectToAction($action, $parameters, $status, $headers );
141
    }
142
143
    /**
144
     * Create a new redirect response, while putting the current URL in the session.
145
     *
146
     * @param  string  $path
147
     * @param  int  $status
148
     * @param  array  $headers
149
     * @param  bool|null  $secure
150
     */
151
    public function redirectGuest($path, $status = 302, $headers = [], $secure = null)
152
    {
153
        $this->response = response()->redirectGuest($path, $status, $headers, $secure);
154
    }
155
156
    /**
157
     * Create a new redirect response to the previously intended location.
158
     *
159
     * @param  string  $default
160
     * @param  int  $status
161
     * @param  array  $headers
162
     * @param  bool|null  $secure
163
     */
164
    public function redirectToIntended($default = '/', $status = 302, $headers = [], $secure = null)
165
    {
166
        $this->response = response()->redirectToIntended($default, $status, $headers, $secure );
167
    }
168
169
    public function send(Response $response)
170
    {
171
        $this->response = $response;
172
    }
173
174
    /**
175
     * @return \Closure
176
     */
177
    private function makeListener(): \Closure
178
    {
179
        $resp = $this->response;
180
        $cb = app(YouShouldHave::class)->predicate;
181
182
        if ($this->exception) {
183
            $e = $this->exception;
184
            return function () use ($e, $cb) {
185
                if (!$cb()) {
186
                    throw $e;
187
                }
188
            };
189
        }
190
191
        return function () use ($resp, $cb) {
192
            if (!$cb()) {
193
                respondWith($resp);
194
            }
195
        };
196
    }
197
198
    public function weThrowNew($exception, $message= '')
199
    {
200
        $this->exception = new $exception($message);
201
    }
202
203
    public function abort($code, $message = '', array $headers = [])
204
    {
205
        try {
206
            abort($code, $message, $headers);
207
        } catch (\Exception $e) {
208
            $this->exception = $e;
209
        }
210
    }
211
212
    public function weDenyAccess($msg = '')
213
    {
214
        $this->exception = new AuthorizationException($msg);
215
    }
216
217
    public function afterCalling($callback, array $parameters = [])
218
    {
219
        app()->call($callback, $parameters);
220
        return $this;
221
    }
222
223
    public function __destruct()
224
    {
225
        $callbackListener = $this->makeListener();
226
        app(HeyMan::class)->authorizer->startGuarding($callbackListener);
227
    }
228
}