Completed
Push — master ( fd747d...a11c0a )
by Song
03:07
created

Content::getUserData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Layout;
4
5
use Closure;
6
use Encore\Admin\Facades\Admin;
7
use Illuminate\Contracts\Support\Renderable;
8
use Illuminate\Support\Arr;
9
10
class Content implements Renderable
11
{
12
    /**
13
     * Content title.
14
     *
15
     * @var string
16
     */
17
    protected $title = ' ';
18
19
    /**
20
     * Content description.
21
     *
22
     * @var string
23
     */
24
    protected $description = ' ';
25
26
    /**
27
     * Page breadcrumb.
28
     *
29
     * @var array
30
     */
31
    protected $breadcrumb = [];
32
33
    /**
34
     * @var Row[]
35
     */
36
    protected $rows = [];
37
38
    /**
39
     * @var array
40
     */
41
    protected $view;
42
43
    /**
44
     * Content constructor.
45
     *
46
     * @param Closure|null $callback
47
     */
48
    public function __construct(\Closure $callback = null)
49
    {
50
        if ($callback instanceof Closure) {
51
            $callback($this);
52
        }
53
    }
54
55
    /**
56
     * Alias of method `title`.
57
     *
58
     * @param string $header
59
     *
60
     * @return $this
61
     */
62
    public function header($header = '')
63
    {
64
        return $this->title($header);
65
    }
66
67
    /**
68
     * @param string $title
69
     *
70
     * @return $this
71
     */
72
    public function title($title)
73
    {
74
        $this->title = $title;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Set description of content.
81
     *
82
     * @param string $description
83
     *
84
     * @return $this
85
     */
86
    public function description($description = '')
87
    {
88
        $this->description = $description;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Set breadcrumb of content.
95
     *
96
     * @param array ...$breadcrumb
97
     *
98
     * @return $this
99
     */
100
    public function breadcrumb(...$breadcrumb)
101
    {
102
        $this->validateBreadcrumb($breadcrumb);
103
104
        $this->breadcrumb = (array) $breadcrumb;
105
106
        return $this;
107
    }
108
109
    /**
110
     * Validate content breadcrumb.
111
     *
112
     * @param array $breadcrumb
113
     *
114
     * @throws \Exception
115
     *
116
     * @return bool
117
     */
118
    protected function validateBreadcrumb(array $breadcrumb)
119
    {
120
        foreach ($breadcrumb as $item) {
121
            if (!is_array($item) || !Arr::has($item, 'text')) {
122
                throw new  \Exception('Breadcrumb format error!');
123
            }
124
        }
125
126
        return true;
127
    }
128
129
    /**
130
     * Alias of method row.
131
     *
132
     * @param mixed $content
133
     *
134
     * @return $this
135
     */
136
    public function body($content)
137
    {
138
        return $this->row($content);
139
    }
140
141
    /**
142
     * Add one row for content body.
143
     *
144
     * @param $content
145
     *
146
     * @return $this
147
     */
148
    public function row($content)
149
    {
150
        if ($content instanceof Closure) {
151
            $row = new Row();
152
            call_user_func($content, $row);
153
            $this->addRow($row);
154
        } else {
155
            $this->addRow(new Row($content));
156
        }
157
158
        return $this;
159
    }
160
161
    /**
162
     * Render giving view as content body.
163
     *
164
     * @param string $view
165
     * @param array  $data
166
     *
167
     * @return $this
168
     */
169
    public function view($view, $data = [])
170
    {
171
        $this->view = compact('view', 'data');
172
173
        return $this;
174
    }
175
176
    /**
177
     * @param $var
178
     *
179
     * @return $this
180
     */
181
    public function dump($var)
0 ignored issues
show
Unused Code introduced by
The parameter $var is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
182
    {
183
        return $this->row(admin_dump(...func_get_args()));
184
    }
185
186
    /**
187
     * Add Row.
188
     *
189
     * @param Row $row
190
     */
191
    protected function addRow(Row $row)
192
    {
193
        $this->rows[] = $row;
194
    }
195
196
    /**
197
     * Build html of content.
198
     *
199
     * @return string
200
     */
201
    public function build()
202
    {
203
        ob_start();
204
205
        foreach ($this->rows as $row) {
206
            $row->build();
207
        }
208
209
        $contents = ob_get_contents();
210
211
        ob_end_clean();
212
213
        return $contents;
214
    }
215
216
    /**
217
     * Set success message for content.
218
     *
219
     * @param string $title
220
     * @param string $message
221
     *
222
     * @return $this
223
     */
224
    public function withSuccess($title = '', $message = '')
225
    {
226
        admin_success($title, $message);
227
228
        return $this;
229
    }
230
231
    /**
232
     * Set error message for content.
233
     *
234
     * @param string $title
235
     * @param string $message
236
     *
237
     * @return $this
238
     */
239
    public function withError($title = '', $message = '')
240
    {
241
        admin_error($title, $message);
242
243
        return $this;
244
    }
245
246
    /**
247
     * Set warning message for content.
248
     *
249
     * @param string $title
250
     * @param string $message
251
     *
252
     * @return $this
253
     */
254
    public function withWarning($title = '', $message = '')
255
    {
256
        admin_warning($title, $message);
257
258
        return $this;
259
    }
260
261
    /**
262
     * Set info message for content.
263
     *
264
     * @param string $title
265
     * @param string $message
266
     *
267
     * @return $this
268
     */
269
    public function withInfo($title = '', $message = '')
270
    {
271
        admin_info($title, $message);
272
273
        return $this;
274
    }
275
276
    /**
277
     * @return array
278
     */
279
    protected function getUserData()
280
    {
281
        if (!$user = Admin::user()) {
282
            return [];
283
        }
284
285
        return Arr::only($user->toArray(), ['id', 'username', 'email', 'name', 'avatar']);
286
    }
287
288
    /**
289
     * Render this content.
290
     *
291
     * @return string
292
     */
293
    public function render()
294
    {
295
        $items = [
296
            'header'      => $this->title,
297
            'description' => $this->description,
298
            'breadcrumb'  => $this->breadcrumb,
299
            '_content_'   => $this->build(),
300
            '_view_'      => $this->view,
301
            '_user_'      => $this->getUserData(),
302
        ];
303
304
        return view('admin::content', $items)->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
305
    }
306
}
307