@@ -44,7 +44,7 @@ trait FileRelations
44
44
* ],
45
45
* ],
46
46
* ]
47
- * </code>
47
+ * </code>.
48
48
*/
49
49
private array $ defaultParams = [
50
50
'limit ' => 1 ,
@@ -85,7 +85,7 @@ public function saving(Saving $event)
85
85
public function saved (Saved $ event )
86
86
{
87
87
foreach ($ this ->fileRelations ?? [] as $ relationName => $ params ) {
88
- if (!empty ($ this ->filesCache [$ relationName ])) {
88
+ if (! empty ($ this ->filesCache [$ relationName ])) {
89
89
foreach ($ this ->filesCache [$ relationName ] as $ file ) {
90
90
$ this ->processFile ($ file , $ event ->getModel (), $ relationName );
91
91
if ($ params ['limit ' ] === 1 ) {
@@ -120,7 +120,7 @@ private function validateUserData(array $data, array $params, string $relationNa
120
120
}
121
121
if ($ params ['limit ' ] !== 1 ) {
122
122
foreach ($ data as $ item ) {
123
- if (!is_array ($ item ) || empty ($ item ['id ' ])) {
123
+ if (! is_array ($ item ) || empty ($ item ['id ' ])) {
124
124
throw new BadRequestHttpException (
125
125
"Invalid file data, {$ relationName } field should contain array of files with id "
126
126
);
@@ -154,20 +154,18 @@ private function checkLimitedOrRequired(Saving $event, string $relationName, arr
154
154
155
155
/**
156
156
* @param File[] $files
157
- * @param bool $revert
158
- * @return array
159
157
*/
160
158
private function filterActiveFiles (array $ files , bool $ revert = false ): array
161
159
{
162
160
return array_filter (array_map (function (File $ file ) use ($ revert ) {
163
161
$ active = empty ($ file ->deleted_at ) && empty ($ file ->delete_it );
164
- return ($ active === !$ revert ) ? $ file ->id : null ;
162
+ return ($ active === ! $ revert ) ? $ file ->id : null ;
165
163
}, $ files ));
166
164
}
167
165
168
166
private function processFile (File $ file , Model $ model , string $ relationName ): void
169
167
{
170
- if (!$ this ->isAssigned ($ file , $ model , $ relationName )) {
168
+ if (! $ this ->isAssigned ($ file , $ model , $ relationName )) {
171
169
$ this ->assignFileToModel ($ file , $ model , $ relationName );
172
170
$ this ->applyModelSettingsToFile ($ file , $ this ->fileRelations [$ relationName ]);
173
171
}
@@ -188,9 +186,9 @@ private function deleteOtherActiveFiles(File $file, Model $model, string $relati
188
186
189
187
private function isAssigned (File $ file , Model $ model , string $ relationName ): bool
190
188
{
191
- return $ file ->fileable_type === get_class ($ model ) &&
192
- $ file ->fileable_id === $ model ->getKey () &&
193
- $ file ->field_name === $ relationName ;
189
+ return $ file ->fileable_type === get_class ($ model )
190
+ && $ file ->fileable_id === $ model ->getKey ()
191
+ && $ file ->field_name === $ relationName ;
194
192
}
195
193
196
194
private function assignFileToModel (File $ file , Model $ model , string $ relationName ): File
@@ -240,7 +238,7 @@ private function generateImagePresets(File $file, array $params, ConfigInterface
240
238
241
239
private function processFileActions (File $ file ): File
242
240
{
243
- if (!empty ($ file ->offsetGet ('delete_it ' ))) {
241
+ if (! empty ($ file ->offsetGet ('delete_it ' ))) {
244
242
$ file ->deleted_at = Carbon::now ();
245
243
}
246
244
@@ -251,7 +249,7 @@ private function processFileActions(File $file): File
251
249
private function applyActionsOnFile (File $ file , array $ fileData )
252
250
{
253
251
foreach ($ this ->extraActions as $ action ) {
254
- if (!empty ($ fileData [$ action ])) {
252
+ if (! empty ($ fileData [$ action ])) {
255
253
$ file ->offsetSet ($ action , $ fileData [$ action ]);
256
254
}
257
255
}
@@ -270,16 +268,19 @@ private function getAllowedFileModel(Saving $event, string $relationName, array
270
268
271
269
$ fileAllowed = function (Builder $ query ) use ($ event , $ relationName , $ fileAvailableForLink ) {
272
270
$ query
273
- ->where ('fileable_type ' , '= ' , get_class ($ event ->getModel ()))
274
- ->where ('fileable_id ' , '= ' , $ event ->getModel ()->getKey ())
275
- ->where ('field_name ' , '= ' , $ relationName )
271
+ ->where (function (Builder $ q ) use ($ event , $ relationName ) {
272
+ $ q
273
+ ->where ('fileable_type ' , '= ' , get_class ($ event ->getModel ()))
274
+ ->where ('fileable_id ' , '= ' , $ event ->getModel ()->getKey ())
275
+ ->where ('field_name ' , '= ' , $ relationName );
276
+ })
276
277
->orWhere ($ fileAvailableForLink );
277
278
};
278
279
279
280
$ query = File::where ('id ' , $ fileData ['id ' ])->where ($ fileAllowed );
280
281
281
282
$ mimeTypes = $ this ->fileRelations [$ relationName ]['mimeTypes ' ] ?? null ;
282
- if (!empty ($ mimeTypes ) && $ mimeTypes != ['* ' ]) {
283
+ if (! empty ($ mimeTypes ) && $ mimeTypes != ['* ' ]) {
283
284
$ query ->whereIn ('mime ' , $ mimeTypes );
284
285
}
285
286
@@ -294,7 +295,7 @@ private function getAllowedFileModel(Saving $event, string $relationName, array
294
295
throw new BadRequestHttpException (
295
296
"File {$ fileData ['id ' ]} can not to be assigned to {$ relationName }. " .
296
297
'Check you have rights to use this file, ' .
297
- "file not assigned yet and you upload one of following file types: $ allowedFileTypes "
298
+ "file not assigned yet and you upload one of following file types: { $ allowedFileTypes} "
298
299
);
299
300
}
300
301
@@ -315,15 +316,13 @@ private function getAllowedToLinkFileModel(Saving $event, string $relationName,
315
316
}
316
317
};
317
318
318
- $ fileAvailableForLink = function (Builder $ query ) use ($ fileOwnOrFree ) {
319
+ return function (Builder $ query ) use ($ fileOwnOrFree ) {
319
320
$ query
320
321
->whereNull ('fileable_type ' )
321
322
->whereNull ('fileable_id ' )
322
323
->whereNull ('field_name ' )
323
324
->where ($ fileOwnOrFree );
324
325
};
325
-
326
- return $ fileAvailableForLink ;
327
326
}
328
327
329
328
private function getAuth (): CoreAuthenticatable |null
0 commit comments