Total Complexity | 41 |
Total Lines | 358 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like AbstractUploadField often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractUploadField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | abstract class AbstractUploadField extends FormField implements FileHandleField |
||
28 | { |
||
29 | use FileUploadReceiver; |
||
|
|||
30 | use ImprovedUploader; |
||
31 | |||
32 | // Schema needs to be something else than custom otherwise it fails on ajax load because |
||
33 | // we don't have a proper react component |
||
34 | protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_HIDDEN; |
||
35 | protected $schemaComponent = null; |
||
36 | |||
37 | /** |
||
38 | * Set if uploading new files is enabled. |
||
39 | * If false, only existing files can be selected |
||
40 | * |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected $uploadEnabled = true; |
||
44 | |||
45 | /** |
||
46 | * Set if selecting existing files is enabled. |
||
47 | * If false, only new files can be selected. |
||
48 | * |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $attachEnabled = true; |
||
52 | |||
53 | /** |
||
54 | * The number of files allowed for this field |
||
55 | * |
||
56 | * @var null|int |
||
57 | */ |
||
58 | protected $allowedMaxFileNumber = null; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $inputType = 'file'; |
||
64 | |||
65 | /** |
||
66 | * @var bool|null |
||
67 | */ |
||
68 | protected $multiUpload = null; |
||
69 | |||
70 | /** |
||
71 | * Create a new file field. |
||
72 | * |
||
73 | * @param string $name The internal field name, passed to forms. |
||
74 | * @param string $title The field label. |
||
75 | * @param SS_List $items Items assigned to this field |
||
76 | */ |
||
77 | public function __construct($name, $title = null, SS_List $items = null) |
||
78 | { |
||
79 | $this->constructFileUploadReceiver(); |
||
80 | |||
81 | // NEW : Reset default size to allow our default config to work properly |
||
82 | $this->getUpload()->getValidator()->allowedMaxFileSize = []; |
||
83 | |||
84 | // When creating new files, rename on conflict |
||
85 | $this->getUpload()->setReplaceFile(false); |
||
86 | |||
87 | parent::__construct($name, $title); |
||
88 | if ($items) { |
||
89 | $this->setItems($items); |
||
90 | } |
||
91 | |||
92 | // NEW : Fix null request |
||
93 | if ($this->request instanceof NullHTTPRequest) { |
||
94 | $this->request = Controller::curr()->getRequest(); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | public function getSchemaDataDefaults() |
||
99 | { |
||
100 | $defaults = parent::getSchemaDataDefaults(); |
||
101 | |||
102 | // NEW : wrap conditionnaly to avoid errors if not linked to a form |
||
103 | if ($this->form) { |
||
104 | $uploadLink = $this->Link('upload'); |
||
105 | $defaults['data']['createFileEndpoint'] = [ |
||
106 | 'url' => $uploadLink, |
||
107 | 'method' => 'post', |
||
108 | 'payloadFormat' => 'urlencoded', |
||
109 | ]; |
||
110 | } |
||
111 | |||
112 | $defaults['data']['maxFilesize'] = $this->getAllowedMaxFileSize() / 1024 / 1024; |
||
113 | $defaults['data']['maxFiles'] = $this->getAllowedMaxFileNumber(); |
||
114 | $defaults['data']['multi'] = $this->getIsMultiUpload(); |
||
115 | $defaults['data']['parentid'] = $this->getFolderID(); |
||
116 | $defaults['data']['canUpload'] = $this->getUploadEnabled(); |
||
117 | $defaults['data']['canAttach'] = $this->getAttachEnabled(); |
||
118 | |||
119 | return $defaults; |
||
120 | } |
||
121 | |||
122 | |||
123 | /** |
||
124 | * Handles file uploading |
||
125 | * |
||
126 | * @param HTTPRequest $request |
||
127 | * @return HTTPResponse |
||
128 | */ |
||
129 | abstract public function upload(HTTPRequest $request); |
||
130 | |||
131 | /** |
||
132 | * Get ID of target parent folder |
||
133 | * |
||
134 | * @return int |
||
135 | */ |
||
136 | protected function getFolderID() |
||
137 | { |
||
138 | $folderName = $this->getFolderName(); |
||
139 | if (!$folderName) { |
||
140 | return 0; |
||
141 | } |
||
142 | $folder = Folder::find_or_make($folderName); |
||
143 | return $folder ? $folder->ID : 0; |
||
144 | } |
||
145 | |||
146 | public function getSchemaStateDefaults() |
||
147 | { |
||
148 | $state = parent::getSchemaStateDefaults(); |
||
149 | $state['data']['files'] = $this->getItemIDs(); |
||
150 | $state['value'] = $this->Value() ?: ['Files' => []]; |
||
151 | return $state; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Check if allowed to upload more than one file |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function getIsMultiUpload() |
||
160 | { |
||
161 | if (isset($this->multiUpload)) { |
||
162 | return $this->multiUpload; |
||
163 | } |
||
164 | // Guess from record |
||
165 | $record = $this->getRecord(); |
||
166 | $name = $this->getName(); |
||
167 | |||
168 | // Disabled for has_one components |
||
169 | if ($record && DataObject::getSchema()->hasOneComponent(get_class($record), $name)) { |
||
170 | return false; |
||
171 | } |
||
172 | return true; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Set upload type to multiple or single |
||
177 | * |
||
178 | * @param bool $bool True for multiple, false for single |
||
179 | * @return $this |
||
180 | */ |
||
181 | public function setIsMultiUpload($bool) |
||
182 | { |
||
183 | $this->multiUpload = $bool; |
||
184 | return $this; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Gets the number of files allowed for this field |
||
189 | * |
||
190 | * @return null|int |
||
191 | */ |
||
192 | public function getAllowedMaxFileNumber() |
||
193 | { |
||
194 | return $this->allowedMaxFileNumber; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Returns the max allowed filesize |
||
199 | * |
||
200 | * @return null|int |
||
201 | */ |
||
202 | public function getAllowedMaxFileSize() |
||
203 | { |
||
204 | return $this->getValidator()->getLargestAllowedMaxFileSize(); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @return boolean |
||
209 | */ |
||
210 | public function isDefaultMaxFileSize() |
||
211 | { |
||
212 | // This returns null until getAllowedMaxFileSize is called |
||
213 | $current = $this->getValidator()->getLargestAllowedMaxFileSize(); |
||
214 | return $current ? false : true; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Sets the number of files allowed for this field |
||
219 | * @param $count |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function setAllowedMaxFileNumber($count) |
||
223 | { |
||
224 | $this->allowedMaxFileNumber = $count; |
||
225 | |||
226 | return $this; |
||
227 | } |
||
228 | |||
229 | public function getAttributes() |
||
230 | { |
||
231 | $attributes = array( |
||
232 | 'class' => $this->extraClass(), |
||
233 | 'type' => 'file', |
||
234 | 'multiple' => $this->getIsMultiUpload(), |
||
235 | 'id' => $this->ID(), |
||
236 | 'data-schema' => json_encode($this->getSchemaData()), |
||
237 | 'data-state' => json_encode($this->getSchemaState()), |
||
238 | ); |
||
239 | |||
240 | $attributes = array_merge($attributes, $this->attributes); |
||
241 | |||
242 | $this->extend('updateAttributes', $attributes); |
||
243 | |||
244 | return $attributes; |
||
245 | } |
||
246 | |||
247 | public function Type() |
||
250 | } |
||
251 | |||
252 | public function performReadonlyTransformation() |
||
253 | { |
||
254 | $clone = clone $this; |
||
255 | $clone->setReadonly(true); |
||
256 | return $clone; |
||
257 | } |
||
258 | |||
259 | public function performDisabledTransformation() |
||
260 | { |
||
261 | $clone = clone $this; |
||
262 | $clone->setDisabled(true); |
||
263 | return $clone; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Checks if the number of files attached adheres to the $allowedMaxFileNumber defined |
||
268 | * |
||
269 | * @param Validator $validator |
||
270 | * @return bool |
||
271 | */ |
||
272 | public function validate($validator) |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Check if uploading files is enabled |
||
294 | * |
||
295 | * @return bool |
||
296 | */ |
||
297 | public function getUploadEnabled() |
||
298 | { |
||
299 | return $this->uploadEnabled; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Set if uploading files is enabled |
||
304 | * |
||
305 | * @param bool $uploadEnabled |
||
306 | * @return $this |
||
307 | */ |
||
308 | public function setUploadEnabled($uploadEnabled) |
||
309 | { |
||
310 | $this->uploadEnabled = $uploadEnabled; |
||
311 | return $this; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Check if attaching files is enabled |
||
316 | * |
||
317 | * @return bool |
||
318 | */ |
||
319 | public function getAttachEnabled() |
||
320 | { |
||
321 | return $this->attachEnabled; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Set if attaching files is enabled |
||
326 | * |
||
327 | * @param bool $attachEnabled |
||
328 | * @return UploadField |
||
329 | */ |
||
330 | public function setAttachEnabled($attachEnabled) |
||
331 | { |
||
332 | $this->attachEnabled = $attachEnabled; |
||
333 | return $this; |
||
334 | } |
||
335 | |||
336 | public function Field($properties = array()) |
||
337 | { |
||
338 | $record = $this->getRecord(); |
||
339 | if ($record) { |
||
340 | $relation = $record->getRelationClass($this->name); |
||
341 | |||
342 | // Make sure images do not accept default stuff |
||
343 | if ($relation == Image::class) { |
||
344 | $allowedExtensions = $this->getAllowedExtensions(); |
||
345 | if (in_array('zip', $allowedExtensions)) { |
||
346 | // Only allow processable file types for images by default |
||
347 | $this->setAllowedExtensions(['jpg', 'jpeg', 'png']); |
||
348 | } |
||
349 | } |
||
350 | |||
351 | // Set a default description if none set |
||
352 | if (!$this->description && static::config()->enable_default_description) { |
||
353 | $this->setDefaultDescription($relation, $record, $this->name); |
||
354 | } |
||
355 | } |
||
356 | return parent::Field($properties); |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Gets the upload folder name |
||
361 | * |
||
362 | * @return string |
||
363 | */ |
||
364 | public function getFolderName() |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * @inheritDoc |
||
373 | */ |
||
374 | public function Link($action = null) |
||
375 | { |
||
385 | } |
||
386 | } |
||
387 |