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

File::minSize()   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 Method

Rating   Name   Duplication   Size   Complexity  
A File::getSaveCallback() 0 3 1
1
<?php
2
3
namespace SleepingOwl\Admin\Form\Element;
4
5
use Closure;
6
use Illuminate\Http\UploadedFile;
7
use Illuminate\Routing\Router;
8
use Illuminate\Support\Arr;
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 SleepingOwl\Admin\Contracts\WithRoutesInterface;
11
use SleepingOwl\Admin\Traits\FileSize;
12
13
class File extends NamedFormElement implements WithRoutesInterface
14
{
15
    use FileSize;
16
17
    /**
18
     * @var string
19
     */
20
    protected static $route = 'file';
21
22
    /**
23
     * @var \Closure
24
     */
25
    protected $saveCallback;
26
27
    /**
28
     * @param Router $router
29
     */
30
    public static function registerRoutes(Router $router)
31
    {
32
        $routeName = 'admin.form.element.'.static::$route;
33
34
        if (! $router->has($routeName)) {
35
            $router->post('{adminModel}/'.static::$route.'/{field?}/{id?}', [
36
                'as' => $routeName,
37
                'uses' => 'SleepingOwl\Admin\Http\Controllers\UploadController@fromField',
38
            ]);
39
        }
40
    }
41
42
    /**
43
     * @var string
44
     */
45
    protected $driver = 'file';
46
47
    /**
48
     * @var array
49
     */
50
    protected $driverOptions = [];
51
52
    /**
53
     * @var Closure
54
     */
55
    protected $uploadPath;
56
57
    /**
58
     * @var Closure
59
     */
60
    protected $uploadFileName;
61
62
    /**
63
     * @var array
64
     */
65
    protected $uploadSettings = [];
66
67
    /**
68
     * @var array
69
     */
70
    protected $uploadValidationRules = ['required', 'file'];
71
72
    /**
73
     * @var string
74
     */
75
    protected $view = 'form.element.file';
76
77
    /**
78
     * @return array
79
     */
80
    public function getUploadValidationMessages()
81
    {
82
        $messages = [];
83
        foreach ($this->validationMessages as $rule => $message) {
84
            $messages["file.{$rule}"] = $message;
85
        }
86
87
        return $messages;
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function getUploadValidationLabels()
94
    {
95
        return ['file' => $this->getLabel()];
96
    }
97
98
    /**
99
     * @param $driver
100
     * @param array $driverOptions
101
     * @return $this
102
     */
103
    public function setDriver($driver, $driverOptions = [])
104
    {
105
        $this->driver = $driver;
106
        $this->driverOptions = $driverOptions;
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    public function getDriver()
115
    {
116
        return ['driver' => $this->driver, 'driverOptions' => $this->driverOptions];
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    public function getUploadValidationRules()
123
    {
124
        return ['file' => array_unique($this->uploadValidationRules)];
125
    }
126
127
    /**
128
     * @param UploadedFile $file
129
     *
130
     * @return mixed
131
     */
132
    public function getUploadPath(UploadedFile $file)
133
    {
134
        if (! is_callable($this->uploadPath)) {
135
            return $this->defaultUploadPath($file);
136
        }
137
138
        return call_user_func($this->uploadPath, $file);
139
    }
140
141
    /**
142
     * @param Closure $uploadPath
143
     *
144
     * @return $this
145
     */
146
    public function setUploadPath(Closure $uploadPath)
147
    {
148
        $this->uploadPath = $uploadPath;
149
150
        return $this;
151
    }
152
153
    /**
154
     * @param UploadedFile $file
155
     *
156
     * @return string
157
     */
158
    public function getUploadFileName(UploadedFile $file)
159
    {
160
        if (! is_callable($this->uploadFileName)) {
161
            return $this->defaultUploadFilename($file);
162
        }
163
164
        return call_user_func($this->uploadFileName, $file);
165
    }
166
167
    /**
168
     * @param Closure $uploadFileName
169
     *
170
     * @return $this
171
     */
172
    public function setUploadFileName(Closure $uploadFileName)
173
    {
174
        $this->uploadFileName = $uploadFileName;
175
176
        return $this;
177
    }
178
179
    /**
180
     * @return array
181
     */
182
    public function getUploadSettings()
183
    {
184
        if (empty($this->uploadSettings) && in_array(Upload::class, class_uses($this->getModel()))) {
185
            return (array) Arr::get($this->getModel()->getUploadSettings(), $this->getPath());
0 ignored issues
show
Bug introduced by
It seems like $this->getModel()->getUploadSettings() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $array of Illuminate\Support\Arr::get() does only seem to accept ArrayAccess|array, 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

185
            return (array) Arr::get(/** @scrutinizer ignore-type */ $this->getModel()->getUploadSettings(), $this->getPath());
Loading history...
186
        }
187
188
        return $this->uploadSettings;
189
    }
190
191
    /**
192
     * @param array $imageSettings
193
     *
194
     * @return $this
195
     */
196
    public function setUploadSettings(array $imageSettings)
197
    {
198
        $this->uploadSettings = $imageSettings;
199
200
        return $this;
201
    }
202
203
    /**
204
     * @param string $rule
205
     * @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...
206
     * @return $this|\SleepingOwl\Admin\Form\Element\File|\SleepingOwl\Admin\Form\Element\NamedFormElement
207
     */
208
    public function addValidationRule($rule, $message = null)
209
    {
210
        $uploadRules = ['file', 'image', 'mime', 'size', 'dimensions', 'max', 'min', 'between'];
211
212
        foreach ($uploadRules as $uploadRule) {
213
            if (strpos($rule, $uploadRule) !== false) {
214
                $this->uploadValidationRules[] = $rule;
215
216
                if (is_null($message)) {
217
                    return $this;
218
                }
219
220
                return $this->addValidationMessage($rule, $message);
221
            }
222
        }
223
224
        return parent::addValidationRule($rule, $message);
225
    }
226
227
    /**
228
     * @param \Closure $callable
229
     * @return $this
230
     */
231
    public function setSaveCallback(\Closure $callable)
232
    {
233
        $this->saveCallback = $callable;
234
235
        return $this;
236
    }
237
238
    /**
239
     * Return save callback.
240
     * @return \Closure
241
     */
242
    public function getSaveCallback()
243
    {
244
        return $this->saveCallback;
245
    }
246
247
    /**
248
     * @param UploadedFile $file
249
     * @param string $path
250
     * @param string $filename
251
     * @param array $settings
252
     * @return \Closure|array
253
     */
254
    public function saveFile(UploadedFile $file, $path, $filename, array $settings)
255
    {
256
        if (is_callable($callback = $this->getSaveCallback())) {
257
            return $callback($file, $path, $filename, $settings);
258
        }
259
260
        $file->move($path, $filename);
261
262
        //S3 Implement
263
        $value = $path.'/'.$filename;
264
265
        return ['path' => asset($value), 'value' => $value];
266
    }
267
268
    /**
269
     * @param \Illuminate\Validation\Validator $validator
270
     */
271
    public function customValidation(\Illuminate\Validation\Validator $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed. ( Ignorable by Annotation )

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

271
    public function customValidation(/** @scrutinizer ignore-unused */ \Illuminate\Validation\Validator $validator)

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

Loading history...
272
    {
273
    }
274
275
    /**
276
     * @param UploadedFile $file
277
     *
278
     * @return string
279
     */
280
    public function defaultUploadFilename(UploadedFile $file)
281
    {
282
        return md5(time().$file->getClientOriginalName()).'.'.$file->getClientOriginalExtension();
283
    }
284
285
    /**
286
     * @param UploadedFile $file
287
     *
288
     * @return string
289
     */
290
    public function defaultUploadPath(UploadedFile $file)
291
    {
292
        return config('sleeping_owl.filesUploadDirectory', 'files/uploads');
293
    }
294
295
    /**
296
     * @return array
297
     */
298
    public function toArray()
299
    {
300
        $return = parent::toArray();
301
302
        return $return;
303
    }
304
}
305