Completed
Push — master ( 1db3b4...e2f1ad )
by Song
02:46
created

Response::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 13
rs 9.8333
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 download response.
157
     *
158
     * @param string $url
159
     *
160
     * @return $this
161
     */
162
    public function download($url)
163
    {
164
        $this->then = ['action' => 'download', 'value' => $url];
165
166
        return $this;
167
    }
168
169
    /**
170
     * Send a refresh response.
171
     *
172
     * @return $this
173
     */
174
    public function refresh()
175
    {
176
        $this->then = ['action' => 'refresh', 'value' => true];
177
178
        return $this;
179
    }
180
181
    /**
182
     * Send a html response.
183
     *
184
     * @param string $html
185
     *
186
     * @return $this
187
     */
188
    public function html($html = '')
189
    {
190
        $this->html = $html;
191
192
        return $this;
193
    }
194
195
    /**
196
     * @param \Exception $exception
197
     *
198
     * @return mixed
199
     */
200
    public static function withException(\Exception $exception)
201
    {
202
        $response = new static();
203
204
        $response->status = false;
205
206
        if ($exception instanceof ValidationException) {
207
            $message = collect($exception->errors())->flatten()->implode("\n");
208
        } else {
209
            $message = $exception->getMessage();
210
        }
211
212
        return $response->toastr()->topCenter()->error($message);
213
    }
214
215
    /**
216
     * @return \Illuminate\Http\JsonResponse
217
     */
218
    public function send()
219
    {
220
        $data = array_merge(
221
            ['status' => $this->status, 'then' => $this->then],
222
            $this->getPlugin()->getOptions()
223
        );
224
225
        if ($this->html) {
226
            $data['html'] = $this->html;
227
        }
228
229
        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...
230
    }
231
232
    /**
233
     * @param string $method
234
     * @param array  $arguments
235
     *
236
     * @return $this
237
     */
238
    public function __call($method, $arguments)
239
    {
240
        if (in_array($method, $this->toastrMethods)) {
241
            $this->toastr();
242
        }
243
244
        $this->getPlugin()->{$method}(...$arguments);
245
246
        return $this;
247
    }
248
}
249