Completed
Push — master ( f936b6...0a339b )
by Song
02:20
created

Response::location()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Actions;
4
5
use Illuminate\Validation\ValidationException;
6
7
/**
8
 * Class Response.
9
 *
10
 * @method $this topCenter()
11
 * @method $this topLeft()
12
 * @method $this topRight()
13
 * @method $this bottomLeft()
14
 * @method $this bottomCenter()
15
 * @method $this bottomRight()
16
 * @method $this topFullWidth()
17
 * @method $this bottomFullWidth()
18
 * @method $this timeout($timeout = 5000)
19
 */
20
class Response
21
{
22
    /**
23
     * @var bool
24
     */
25
    public $status = true;
26
27
    /**
28
     * @var \Exception
29
     */
30
    public $exception;
31
32
    /**
33
     * @var array
34
     */
35
    public $toastrMethods = [
36
        'topCenter', 'topLeft', 'topRight',
37
        'bottomLeft', 'bottomCenter', 'bottomRight',
38
        'topFullWidth', 'bottomFullWidth', 'timeout',
39
    ];
40
41
    /**
42
     * @var
43
     */
44
    protected $plugin;
45
46
    /**
47
     * @var array
48
     */
49
    protected $then = [];
50
51
    /**
52
     * @var string
53
     */
54
    protected $html = '';
55
56
    /**
57
     * @return $this
58
     */
59
    public function toastr()
60
    {
61
        if (!$this->plugin instanceof Toastr) {
62
            $this->plugin = new Toastr();
63
        }
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return $this
70
     */
71
    public function swal()
72
    {
73
        if (!$this->plugin instanceof SweatAlert2) {
74
            $this->plugin = new SweatAlert2();
75
        }
76
77
        return $this;
78
    }
79
80
    /**
81
     * @return SweatAlert2
82
     */
83
    public function getPlugin()
84
    {
85
        return $this->plugin;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->plugin; of type Encore\Admin\Actions\Toa...min\Actions\SweatAlert2 adds the type Encore\Admin\Actions\Toastr to the return on line 85 which is incompatible with the return type documented by Encore\Admin\Actions\Response::getPlugin of type Encore\Admin\Actions\SweatAlert2.
Loading history...
86
    }
87
88
    /**
89
     * @param string $message
90
     *
91
     * @return $this
92
     */
93
    public function success(string $message = '')
94
    {
95
        return $this->show('success', $message);
96
    }
97
98
    /**
99
     * @param string $message
100
     *
101
     * @return $this
102
     */
103
    public function info(string $message = '')
104
    {
105
        return $this->show('info', $message);
106
    }
107
108
    /**
109
     * @param string $message
110
     *
111
     * @return $this
112
     */
113
    public function warning(string $message = '')
114
    {
115
        return $this->show('warning', $message);
116
    }
117
118
    /**
119
     * @param string $message
120
     *
121
     * @return $this
122
     */
123
    public function error(string $message = '')
124
    {
125
        return $this->show('error', $message);
126
    }
127
128
    /**
129
     * @param string $type
130
     * @param string $title
131
     *
132
     * @return $this
133
     */
134
    protected function show($type, $title = '')
135
    {
136
        $this->getPlugin()->show($type, $title);
137
138
        return $this;
139
    }
140
141
    /**
142
     * Send a redirect response.
143
     *
144
     * @param string $url
145
     *
146
     * @return $this
147
     */
148
    public function redirect(string $url)
149
    {
150
        $this->then = ['action' => 'redirect', 'value' => $url];
151
152
        return $this;
153
    }
154
155
    /**
156
     * Send a location redirect response.
157
     *
158
     * @param string $location
159
     *
160
     * @return $this
161
     */
162
    public function location(string $location)
163
    {
164
        $this->then = ['action' => 'location', 'value' => $location];
165
166
        return $this;
167
    }
168
169
    /**
170
     * Send a download response.
171
     *
172
     * @param string $url
173
     *
174
     * @return $this
175
     */
176
    public function download($url)
177
    {
178
        $this->then = ['action' => 'download', 'value' => $url];
179
180
        return $this;
181
    }
182
183
    /**
184
     * Send a refresh response.
185
     *
186
     * @return $this
187
     */
188
    public function refresh()
189
    {
190
        $this->then = ['action' => 'refresh', 'value' => true];
191
192
        return $this;
193
    }
194
195
    /**
196
     * Send a html response.
197
     *
198
     * @param string $html
199
     *
200
     * @return $this
201
     */
202
    public function html($html = '')
203
    {
204
        $this->html = $html;
205
206
        return $this;
207
    }
208
209
    /**
210
     * @param \Exception $exception
211
     *
212
     * @return mixed
213
     */
214
    public static function withException(\Exception $exception)
215
    {
216
        $response = new static();
217
218
        $response->status = false;
219
220
        if ($exception instanceof ValidationException) {
221
            $message = collect($exception->errors())->flatten()->implode("\n");
222
        } else {
223
            $message = $exception->getMessage();
224
        }
225
226
        return $response->toastr()->topCenter()->error($message);
227
    }
228
229
    /**
230
     * @return \Illuminate\Http\JsonResponse
231
     */
232
    public function send()
233
    {
234
        $data = array_merge(
235
            ['status' => $this->status, 'then' => $this->then],
236
            $this->getPlugin()->getOptions()
237
        );
238
239
        if ($this->html) {
240
            $data['html'] = $this->html;
241
        }
242
243
        return response()->json($data);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
244
    }
245
246
    /**
247
     * @param string $method
248
     * @param array  $arguments
249
     *
250
     * @return $this
251
     */
252
    public function __call($method, $arguments)
253
    {
254
        if (in_array($method, $this->toastrMethods)) {
255
            $this->toastr();
256
        }
257
258
        $this->getPlugin()->{$method}(...$arguments);
259
260
        return $this;
261
    }
262
}
263