GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — analysis-8PZYVR ( 0b10a7 )
by butschster
09:32
created

Files::setTitleRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Form\Element;
4
5
use Closure;
6
use Illuminate\Support\Arr;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\UploadedFile;
9
use KodiComponents\Support\Upload;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, SleepingOwl\Admin\Form\Element\Upload. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use Illuminate\Support\Facades\File;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, SleepingOwl\Admin\Form\Element\File. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
12
class Files extends Images
13
{
14
    protected $uploadValidationRules = ['required'];
15
16
    protected $view = 'form.element.files';
17
18
    protected $files_group_class = null;
19
20
    protected $show_title = true;
21
22
    protected $show_description = true;
23
24
    protected $title_required = false;
25
26
    protected $description_required = false;
27
28
    /**
29
     * @param bool $bool
30
     *
31
     * @return $this
32
     */
33
    public function showTitle($bool)
34
    {
35
        $this->show_title = $bool;
36
37
        return $this;
38
    }
39
40
    /**
41
     * @param bool $bool
42
     *
43
     * @return $this
44
     */
45
    public function showDescription($bool)
46
    {
47
        $this->show_description = $bool;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param bool $bool
54
     *
55
     * @return $this
56
     */
57
    public function setTitleRequired($bool)
58
    {
59
        $this->title_required = $bool;
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param bool $bool
66
     *
67
     * @return $this
68
     */
69
    public function setDescriptionRequired($bool)
70
    {
71
        $this->description_required = $bool;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getUploadValidationMessages()
80
    {
81
        $messages = [];
82
        foreach ($this->validationMessages as $rule => $message) {
83
            $messages["file.{$rule}"] = $message;
84
        }
85
86
        return $messages;
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function getUploadValidationLabels()
93
    {
94
        return ['file' => $this->getLabel()];
95
    }
96
97
    /**
98
     * @param $driver
99
     * @param array $driverOptions
100
     * @return $this
101
     */
102
    public function setDriver($driver, $driverOptions = [])
103
    {
104
        $this->driver = $driver;
105
        $this->driverOptions = $driverOptions;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return array
112
     */
113
    public function getDriver()
114
    {
115
        return ['driver' => $this->driver, 'driverOptions' => $this->driverOptions];
116
    }
117
118
    /**
119
     * @return array
120
     */
121
    public function getUploadValidationRules()
122
    {
123
        return ['file' => array_unique($this->uploadValidationRules)];
124
    }
125
126
    /**
127
     * @param UploadedFile $file
128
     *
129
     * @return mixed
130
     */
131
    public function getUploadPath(UploadedFile $file)
132
    {
133
        if (! is_callable($this->uploadPath)) {
134
            return $this->defaultUploadPath($file);
135
        }
136
137
        return call_user_func($this->uploadPath, $file);
138
    }
139
140
    /**
141
     * @param Closure $uploadPath
142
     *
143
     * @return $this
144
     */
145
    public function setUploadPath(Closure $uploadPath)
146
    {
147
        $this->uploadPath = $uploadPath;
148
149
        return $this;
150
    }
151
152
    /**
153
     * @param UploadedFile $file
154
     *
155
     * @return string
156
     */
157
    public function getUploadFileName(UploadedFile $file)
158
    {
159
        if (! is_callable($this->uploadFileName)) {
160
            return $this->defaultUploadFilename($file);
161
        }
162
163
        return call_user_func($this->uploadFileName, $file);
164
    }
165
166
    /**
167
     * @param Closure $uploadFileName
168
     *
169
     * @return $this
170
     */
171
    public function setUploadFileName(Closure $uploadFileName)
172
    {
173
        $this->uploadFileName = $uploadFileName;
174
175
        return $this;
176
    }
177
178
    /**
179
     * @return array
180
     */
181
    public function getUploadSettings()
182
    {
183
        if (empty($this->uploadSettings) && in_array(Upload::class, class_uses($this->getModel()))) {
184
            return (array) array_get($this->getModel()->getUploadSettings(), $this->getPath());
0 ignored issues
show
Bug introduced by
The function array_get was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

184
            return (array) /** @scrutinizer ignore-call */ array_get($this->getModel()->getUploadSettings(), $this->getPath());
Loading history...
185
        }
186
187
        return $this->uploadSettings;
188
    }
189
190
    /**
191
     * @param array $imageSettings
192
     *
193
     * @return $this
194
     */
195
    public function setUploadSettings(array $imageSettings)
196
    {
197
        $this->uploadSettings = $imageSettings;
198
199
        return $this;
200
    }
201
202
    /**
203
     * @param string $rule
204
     * @param null $message
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $message is correct as it would always require null to be passed?
Loading history...
205
     * @return $this|\SleepingOwl\Admin\Form\Element\File|\SleepingOwl\Admin\Form\Element\NamedFormElement
206
     */
207
    public function addValidationRule($rule, $message = null)
208
    {
209
        $uploadRules = ['file', 'image', 'mime', 'size', 'dimensions', 'max', 'min', 'between'];
210
211
        foreach ($uploadRules as $uploadRule) {
212
            if (strpos($rule, $uploadRule) !== false) {
213
                $this->uploadValidationRules[] = $rule;
214
215
                if (is_null($message)) {
216
                    return $this;
217
                }
218
219
                return $this->addValidationMessage($rule, $message);
220
            }
221
        }
222
223
        return parent::addValidationRule($rule, $message);
224
    }
225
226
    /**
227
     * @param int $size Max size in kilobytes
228
     *
229
     * @return $this
230
     */
231
    public function maxSize($size)
232
    {
233
        $this->addValidationRule('max:'.(int) $size);
234
235
        return $this;
236
    }
237
238
    /**
239
     * @param int $size Max size in kilobytes
240
     *
241
     * @return $this
242
     */
243
    public function minSize($size)
244
    {
245
        $this->addValidationRule('min:'.(int) $size);
246
247
        return $this;
248
    }
249
250
    /**
251
     * @param \Closure $callable
252
     * @return $this
253
     */
254
    public function setSaveCallback(\Closure $callable)
255
    {
256
        $this->saveCallback = $callable;
257
258
        return $this;
259
    }
260
261
    /**
262
     * Return save callback.
263
     * @return \Closure
264
     */
265
    public function getSaveCallback()
266
    {
267
        return $this->saveCallback;
268
    }
269
270
    /**
271
     * @param UploadedFile $file
272
     * @param string $path
273
     * @param string $filename
274
     * @param array $settings
275
     * @return \Closure|array
276
     */
277
    public function saveFile(UploadedFile $file, $path, $filename, array $settings)
278
    {
279
        if (is_callable($callback = $this->getSaveCallback())) {
280
            return $callback($file, $path, $filename, $settings);
281
        }
282
283
        $file->move($path, $filename);
284
285
        //S3 Implement
286
        $value = $path.'/'.$filename;
287
288
        return ['path' => asset($value), 'value' => $value];
289
    }
290
291
    /**
292
     * @param \Illuminate\Validation\Validator $validator
293
     */
294
    public function customValidation(\Illuminate\Validation\Validator $validator)
295
    {
296
    }
297
298
    /**
299
     * @param UploadedFile $file
300
     *
301
     * @return string
302
     */
303
    public function defaultUploadFilename(UploadedFile $file)
304
    {
305
        return md5(time().$file->getClientOriginalName()).'.'.$file->getClientOriginalExtension();
306
    }
307
308
    /**
309
     * @param UploadedFile $file
310
     *
311
     * @return string
312
     */
313
    public function defaultUploadPath(UploadedFile $file)
314
    {
315
        return config('sleeping_owl.filesUploadDirectory', 'files/uploads');
316
    }
317
318
    /**
319
     * @return array|mixed|string
320
     */
321
    public function getValueFromModel()
322
    {
323
        $value = $this->model->{$this->name};
324
        $return = isset($value) && mb_strlen($value) >= 5 ? json_decode($value, true) : [];
325
326
        return $return;
327
    }
328
329
    /**
330
     * @param string $mode
331
     *
332
     * @return $this
333
     */
334
    public function setListMode($mode)
335
    {
336
        if ($mode == 'vertical') {
337
            $this->files_group_class = 'files-group-vertical';
338
        } elseif ($mode == 'horizontal') {
339
            $this->files_group_class = null;
340
        }
341
342
        return $this;
343
    }
344
345
    /**
346
     * @return $this
347
     */
348
    public function setVertical()
349
    {
350
        $this->setListMode('vertical');
351
352
        return $this;
353
    }
354
355
    /**
356
     * @return $this
357
     */
358
    public function setHorizontal()
359
    {
360
        $this->setListMode('horizontal');
361
362
        return $this;
363
    }
364
365
    /**
366
     * @param Request $request
367
     */
368
    public function save(Request $request)
369
    {
370
        $name = $this->getName();
371
        $value = Arr::get($request->all(), $this->getNameKey());
372
373
        if (is_array($value_array = json_decode($value, true)) && count($value_array)) {
374
            foreach ($value_array as $v => $array) {
375
                $file = $array['url'];
376
                if ($file && File::exists($file)) {
377
                    if (! isset($array['filesize'])) {
378
                        $array['filesize'] = File::size($file);
379
                    }
380
                    if (! isset($array['ext'])) {
381
                        $array['ext'] = File::extension($file);
382
                    }
383
                    $mime = File::mimeType($file);
384
                    if (! isset($array['mime'])) {
385
                        $array['mime'] = $mime;
386
                    }
387
                    if (mb_strpos($mime, '/')) {
0 ignored issues
show
Bug introduced by
It seems like $mime can also be of type false; however, parameter $haystack of mb_strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

387
                    if (mb_strpos(/** @scrutinizer ignore-type */ $mime, '/')) {
Loading history...
388
                        [$mime1, $mime2] = explode('/', $mime);
0 ignored issues
show
Bug introduced by
It seems like $mime can also be of type false; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

388
                        [$mime1, $mime2] = explode('/', /** @scrutinizer ignore-type */ $mime);
Loading history...
389
                        if (! isset($array['mime_base'])) {
390
                            $array['mime_base'] = $mime1;
391
                        }
392
                        if (! isset($array['mime_detail'])) {
393
                            $array['mime_detail'] = $mime2;
394
                        }
395
                    }
396
                }
397
                $value_array[$v] = $array;
398
            }
399
            $value = json_encode($value_array);
400
        }
401
402
        $request->merge([$name => $value]);
403
404
        $this->setModelAttribute(
405
            $this->getValueFromRequest($request)
406
        );
407
    }
408
409
    /**
410
     * @return array
411
     */
412
    public function toArray()
413
    {
414
        $return = parent::toArray();
415
416
        $return = array_merge($return, [
417
            'files_group_class' => $this->files_group_class,
418
            'show_title' => $this->show_title,
419
            'show_description' => $this->show_description,
420
            'title_required' => $this->title_required,
421
            'description_required' => $this->description_required,
422
        ]);
423
424
        return $return;
425
    }
426
}
427