Completed
Pull Request — master (#1281)
by
unknown
02:32
created

File::prepare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Encore\Admin\Form\Field;
6
use Illuminate\Support\Facades\Validator;
7
use Symfony\Component\HttpFoundation\File\UploadedFile;
8
9
class File extends Field
10
{
11
    use UploadField;
12
13
    /**
14
     * Css.
15
     *
16
     * @var array
17
     */
18
    protected static $css = [
19
        '/vendor/laravel-admin/bootstrap-fileinput/css/fileinput.min.css?v=4.3.7',
20
    ];
21
22
    /**
23
     * Js.
24
     *
25
     * @var array
26
     */
27
    protected static $js = [
28
        '/vendor/laravel-admin/bootstrap-fileinput/js/plugins/canvas-to-blob.min.js?v=4.3.7',
29
        '/vendor/laravel-admin/bootstrap-fileinput/js/fileinput.min.js?v=4.3.7',
30
    ];
31
32
    /**
33
     * Create a new File instance.
34
     *
35
     * @param string $column
36
     * @param array  $arguments
37
     */
38
    public function __construct($column, $arguments = [])
39
    {
40
        $this->initStorage();
41
42
        parent::__construct($column, $arguments);
43
    }
44
45
    /**
46
     * Default directory for file to upload.
47
     *
48
     * @return mixed
49
     */
50
    public function defaultDirectory()
51
    {
52
        return config('admin.upload.directory.file');
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getValidator(array $input)
59
    {
60
        if (request()->has(static::FILE_DELETE_FLAG)) {
61
            return false;
62
        }
63
64
        if ($this->validator) {
65
            return $this->validator->call($this, $input);
66
        }
67
68
        /*
69
         * If has original value, means the form is in edit mode,
70
         * then remove required rule from rules.
71
         */
72
        if ($this->original()) {
73
            $this->removeRule('required');
74
        }
75
76
        /*
77
         * Make input data validatable if the column data is `null`.
78
         */
79
        if (array_has($input, $this->column) && is_null(array_get($input, $this->column))) {
0 ignored issues
show
Bug introduced by
It seems like $this->column can also be of type array; however, array_get() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
80
            $input[$this->column] = '';
81
        }
82
83
        $rules = $attributes = [];
84
85
        if (!$fieldRules = $this->getRules()) {
86
            return false;
87
        }
88
89
        $rules[$this->column] = $fieldRules;
90
        $attributes[$this->column] = $this->label;
91
92
        return Validator::make($input, $rules, $this->validationMessages, $attributes);
93
    }
94
95
    /**
96
     * Prepare for saving.
97
     *
98
     * @param UploadedFile|array $file
99
     *
100
     * @return mixed|string
101
     */
102 View Code Duplication
    public function prepare($file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        if (request()->has(static::FILE_DELETE_FLAG)) {
105
            return $this->destroy();
106
        }
107
108
        $this->name = $this->getStoreName($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by parameter $file on line 102 can also be of type array; however, Encore\Admin\Form\Field\...adField::getStoreName() does only seem to accept object<Symfony\Component...tion\File\UploadedFile>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Documentation Bug introduced by
It seems like $this->getStoreName($file) of type string is incompatible with the declared type null of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
109
110
        return $this->uploadAndDeleteOriginal($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by parameter $file on line 102 can also be of type array; however, Encore\Admin\Form\Field\...loadAndDeleteOriginal() does only seem to accept object<Symfony\Component...tion\File\UploadedFile>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
111
    }
112
113
    /**
114
     * Upload file and delete original file.
115
     *
116
     * @param UploadedFile $file
117
     *
118
     * @return mixed
119
     */
120
    protected function uploadAndDeleteOriginal(UploadedFile $file)
121
    {
122
        $this->renameIfExists($file);
123
124
        $path = $this->storage->putFileAs($this->getDirectory(), $file, $this->name);
125
126
        $this->destroy();
127
128
        return $path;
129
    }
130
131
    /**
132
     * Preview html for file-upload plugin.
133
     *
134
     * @return string
135
     */
136
    protected function preview()
137
    {
138
        return $this->objectUrl($this->value);
139
    }
140
141
    /**
142
     * Initialize the caption.
143
     *
144
     * @param string $caption
145
     *
146
     * @return string
147
     */
148
    protected function initialCaption($caption)
149
    {
150
        return basename($caption);
151
    }
152
153
    /**
154
     * @return array
155
     */
156
    protected function initialPreviewConfig()
157
    {
158
        return [
159
            ['caption' => basename($this->value), 'key' => 0],
160
        ];
161
    }
162
163
    /**
164
     * Render file upload field.
165
     *
166
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
167
     */
168 View Code Duplication
    public function render()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170
        $this->setupDefaultOptions();
171
172
        if (!empty($this->value)) {
173
            $this->attribute('data-initial-preview', $this->preview());
174
175
            $this->setupPreviewOptions();
176
        }
177
178
        $this->options(['overwriteInitial' => true]);
179
180
        $options = json_encode($this->options);
181
182
        $this->script = <<<EOT
183
184
$("input{$this->getElementClassSelector()}").fileinput({$options});
185
186
EOT;
187
188
        return parent::render();
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::render(); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 188 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
189
    }
190
}
191