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-YjgDMd ( da7263 )
by butschster
07:45
created

Files::getUploadPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace SleepingOwl\Admin\Form\Element;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\UploadedFile;
8
use Illuminate\Support\Arr;
9
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...
10
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...
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 \Closure $callable
228
     * @return $this
229
     */
230
    public function setSaveCallback(\Closure $callable)
231
    {
232
        $this->saveCallback = $callable;
233
234
        return $this;
235
    }
236
237
    /**
238
     * Return save callback.
239
     * @return \Closure
240
     */
241
    public function getSaveCallback()
242
    {
243
        return $this->saveCallback;
244
    }
245
246
    /**
247
     * @param UploadedFile $file
248
     * @param string $path
249
     * @param string $filename
250
     * @param array $settings
251
     * @return \Closure|array
252
     */
253
    public function saveFile(UploadedFile $file, $path, $filename, array $settings)
254
    {
255
        if (is_callable($callback = $this->getSaveCallback())) {
256
            return $callback($file, $path, $filename, $settings);
257
        }
258
259
        $file->move($path, $filename);
260
261
        //S3 Implement
262
        $value = $path.'/'.$filename;
263
264
        return ['path' => asset($value), 'value' => $value];
265
    }
266
267
    /**
268
     * @param \Illuminate\Validation\Validator $validator
269
     */
270
    public function customValidation(\Illuminate\Validation\Validator $validator)
271
    {
272
    }
273
274
    /**
275
     * @param UploadedFile $file
276
     *
277
     * @return string
278
     */
279
    public function defaultUploadFilename(UploadedFile $file)
280
    {
281
        return md5(time().$file->getClientOriginalName()).'.'.$file->getClientOriginalExtension();
282
    }
283
284
    /**
285
     * @param UploadedFile $file
286
     *
287
     * @return string
288
     */
289
    public function defaultUploadPath(UploadedFile $file)
290
    {
291
        return config('sleeping_owl.filesUploadDirectory', 'files/uploads');
292
    }
293
294
    /**
295
     * @return array|mixed|string
296
     */
297
    public function getValueFromModel()
298
    {
299
        $value = $this->model->{$this->name};
300
        $return = isset($value) && mb_strlen($value) >= 5 ? json_decode($value, true) : [];
301
302
        return $return;
303
    }
304
305
    /**
306
     * @param string $mode
307
     *
308
     * @return $this
309
     */
310
    public function setListMode($mode)
311
    {
312
        if ($mode == 'vertical') {
313
            $this->files_group_class = 'files-group-vertical';
314
        } elseif ($mode == 'horizontal') {
315
            $this->files_group_class = null;
316
        }
317
318
        return $this;
319
    }
320
321
    /**
322
     * @return $this
323
     */
324
    public function setVertical()
325
    {
326
        $this->setListMode('vertical');
327
328
        return $this;
329
    }
330
331
    /**
332
     * @return $this
333
     */
334
    public function setHorizontal()
335
    {
336
        $this->setListMode('horizontal');
337
338
        return $this;
339
    }
340
341
    /**
342
     * @param Request $request
343
     */
344
    public function save(Request $request)
345
    {
346
        $name = $this->getName();
347
        $value = Arr::get($request->all(), $this->getNameKey());
348
349
        if (is_array($value_array = json_decode($value, true)) && count($value_array)) {
350
            foreach ($value_array as $v => $array) {
351
                $file = $array['url'];
352
                if ($file && File::exists($file)) {
353
                    if (! isset($array['filesize'])) {
354
                        $array['filesize'] = File::size($file);
355
                    }
356
                    if (! isset($array['ext'])) {
357
                        $array['ext'] = File::extension($file);
358
                    }
359
                    $mime = File::mimeType($file);
360
                    if (! isset($array['mime'])) {
361
                        $array['mime'] = $mime;
362
                    }
363
                    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

363
                    if (mb_strpos(/** @scrutinizer ignore-type */ $mime, '/')) {
Loading history...
364
                        [$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

364
                        [$mime1, $mime2] = explode('/', /** @scrutinizer ignore-type */ $mime);
Loading history...
365
                        if (! isset($array['mime_base'])) {
366
                            $array['mime_base'] = $mime1;
367
                        }
368
                        if (! isset($array['mime_detail'])) {
369
                            $array['mime_detail'] = $mime2;
370
                        }
371
                    }
372
                }
373
                $value_array[$v] = $array;
374
            }
375
            $value = json_encode($value_array);
376
        }
377
378
        $request->merge([$name => $value]);
379
380
        $this->setModelAttribute(
381
            $this->getValueFromRequest($request)
382
        );
383
    }
384
385
    /**
386
     * @return array
387
     */
388
    public function toArray()
389
    {
390
        $return = parent::toArray();
391
392
        $return = array_merge($return, [
393
            'files_group_class' => $this->files_group_class,
394
            'show_title' => $this->show_title,
395
            'show_description' => $this->show_description,
396
            'title_required' => $this->title_required,
397
            'description_required' => $this->description_required,
398
        ]);
399
400
        return $return;
401
    }
402
}
403