Completed
Push — master ( 449659...5c359b )
by Song
03:33
created

Response::open()   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 open new window response.
157
     *
158
     * @param string $url
159
     */
160
    public function open(string $url)
161
    {
162
        $this->then = ['action' => 'open', 'value' => $url];
163
164
        return $this;
165
    }
166
167
    /**
168
     * Send a location redirect response.
169
     *
170
     * @param string $location
171
     *
172
     * @return $this
173
     */
174
    public function location(string $location)
175
    {
176
        $this->then = ['action' => 'location', 'value' => $location];
177
178
        return $this;
179
    }
180
181
    /**
182
     * Send a download response.
183
     *
184
     * @param string $url
185
     *
186
     * @return $this
187
     */
188
    public function download($url)
189
    {
190
        $this->then = ['action' => 'download', 'value' => $url];
191
192
        return $this;
193
    }
194
195
    /**
196
     * Send a refresh response.
197
     *
198
     * @return $this
199
     */
200
    public function refresh()
201
    {
202
        $this->then = ['action' => 'refresh', 'value' => true];
203
204
        return $this;
205
    }
206
207
    /**
208
     * Send a html response.
209
     *
210
     * @param string $html
211
     *
212
     * @return $this
213
     */
214
    public function html($html = '')
215
    {
216
        $this->html = $html;
217
218
        return $this;
219
    }
220
221
    /**
222
     * @param \Exception $exception
223
     *
224
     * @return mixed
225
     */
226
    public static function withException(\Exception $exception)
227
    {
228
        $response = new static();
229
230
        $response->status = false;
231
232
        if ($exception instanceof ValidationException) {
233
            $message = collect($exception->errors())->flatten()->implode("\n");
234
        } else {
235
            $message = $exception->getMessage();
236
        }
237
238
        return $response->toastr()->topCenter()->error($message);
239
    }
240
241
    /**
242
     * @return \Illuminate\Http\JsonResponse
243
     */
244
    public function send()
245
    {
246
        $data = array_merge(
247
            ['status' => $this->status, 'then' => $this->then],
248
            $this->getPlugin()->getOptions()
249
        );
250
251
        if ($this->html) {
252
            $data['html'] = $this->html;
253
        }
254
255
        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...
256
    }
257
258
    /**
259
     * @param string $method
260
     * @param array  $arguments
261
     *
262
     * @return $this
263
     */
264
    public function __call($method, $arguments)
265
    {
266
        if (in_array($method, $this->toastrMethods)) {
267
            $this->toastr();
268
        }
269
270
        $this->getPlugin()->{$method}(...$arguments);
271
272
        return $this;
273
    }
274
}
275