| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Created by PhpStorm. |
||||
| 4 | * User: fomvasss |
||||
| 5 | * Date: 25.11.2019 |
||||
| 6 | * Time: 1:03 |
||||
| 7 | */ |
||||
| 8 | |||||
| 9 | namespace Fomvasss\MediaLibraryExtension; |
||||
| 10 | |||||
| 11 | use Illuminate\Database\Eloquent\Model; |
||||
| 12 | use Illuminate\Http\Request; |
||||
|
0 ignored issues
–
show
|
|||||
| 13 | use Illuminate\Http\UploadedFile; |
||||
|
0 ignored issues
–
show
The type
Illuminate\Http\UploadedFile was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 14 | use Spatie\MediaLibrary\Models\Media; |
||||
| 15 | |||||
| 16 | class MediaLibraryManager |
||||
| 17 | { |
||||
| 18 | public function manage(Model $entity, Request $request) |
||||
| 19 | { |
||||
| 20 | if (method_exists($entity, 'getMediaFieldsMultiple') === false) { |
||||
| 21 | throw new \Exception("Method 'getMediaFieldsMultiple' not found in model class " . class_basename($entity)); |
||||
| 22 | } |
||||
| 23 | |||||
| 24 | if (method_exists($entity, 'getMediaFieldsSingle') === false) { |
||||
| 25 | throw new \Exception("Method 'getMediaFieldsSingle' not found in model class " . class_basename($entity)); |
||||
| 26 | } |
||||
| 27 | |||||
| 28 | // Multiple fields |
||||
| 29 | foreach ($entity->getMediaFieldsMultiple() as $field) { |
||||
| 30 | $this->processMultiple($entity, $request, $field); |
||||
| 31 | } |
||||
| 32 | |||||
| 33 | // Single field |
||||
| 34 | foreach ($entity->getMediaFieldsSingle() as $field) { |
||||
| 35 | $this->processSingle($entity, $request, $field); |
||||
| 36 | } |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | public function add(Model $entity, UploadedFile $uploadedFile, string $collectionName): Media |
||||
| 40 | { |
||||
| 41 | $originalName = $uploadedFile->getClientOriginalName(); |
||||
| 42 | |||||
| 43 | $filenameGenerator = config('medialibrary-extension.filename_generator'); |
||||
|
0 ignored issues
–
show
The function
config 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
Loading history...
|
|||||
| 44 | $filename = $filenameGenerator::get($originalName); |
||||
| 45 | |||||
| 46 | // $fileName = pathinfo($originalName, PATHINFO_FILENAME); |
||||
| 47 | // $fileName = \Illuminate\Support\Str::slug($fileName); |
||||
| 48 | // $fileExtension = pathinfo($originalName, PATHINFO_EXTENSION); |
||||
| 49 | |||||
| 50 | return $entity->addMedia($uploadedFile) |
||||
|
0 ignored issues
–
show
|
|||||
| 51 | ->usingFileName($filename) |
||||
| 52 | ->toMediaCollection($collectionName); |
||||
| 53 | } |
||||
| 54 | |||||
| 55 | protected function processMultiple(Model $entity, Request $request, $field) |
||||
| 56 | { |
||||
| 57 | //$request->validate($entity->getMediaFieldsValidation($field)); |
||||
| 58 | $validator = \Validator::make($request->only($field), $entity->getMediaFieldsValidation($field)); |
||||
|
0 ignored issues
–
show
It seems like
$entity->getMediaFieldsValidation($field) can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $rules of Illuminate\Support\Facades\Validator::make() does only seem to accept 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
Loading history...
|
|||||
| 59 | |||||
| 60 | if ($validator->fails()) { |
||||
| 61 | return redirect()->back()->withErrors($validator)->withInput(); |
||||
|
0 ignored issues
–
show
The function
redirect 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
Loading history...
|
|||||
| 62 | } |
||||
| 63 | |||||
| 64 | if ($request->hasFile($field)) { |
||||
| 65 | //$entity->addMultipleMediaFromRequest([$field])->each(function ($fileAdder) use ($field) { |
||||
| 66 | // $fileAdder->toMediaCollection($field); |
||||
| 67 | //}); |
||||
| 68 | foreach ($request->file($field) as $file) { |
||||
| 69 | $this->add($entity, $file, $field); |
||||
| 70 | } |
||||
| 71 | } |
||||
| 72 | |||||
| 73 | $weightSuffix = config('medialibrary-extension.field_suffixes.weight', '_weight'); |
||||
|
0 ignored issues
–
show
The function
config 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
Loading history...
|
|||||
| 74 | if (($weight = $request->get($field . $weightSuffix)) && is_array($weight)) { |
||||
| 75 | foreach ($weight as $key => $value) { |
||||
| 76 | $entity->media()->where('id', $key)->update(['order_column' => $value]); |
||||
| 77 | } |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | $deletedSuffix = config('medialibrary-extension.field_suffixes.deleted', '_deleted'); |
||||
| 81 | if (($ids = $request->get($field . $deletedSuffix)) && is_array($ids)) { |
||||
| 82 | array_map(function($id) use ($entity) { |
||||
| 83 | $id ? $entity->deleteMedia($id) : null; |
||||
| 84 | }, $ids); |
||||
| 85 | } |
||||
| 86 | //$entity->media()->whereIn('id', $ids)->delete(); |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | protected function processSingle(Model $entity, Request $request, $field) |
||||
| 90 | { |
||||
| 91 | //$request->validate($entity->getMediaFieldsValidation($field)); |
||||
| 92 | $validator = \Validator::make($request->only($field), $entity->getMediaFieldsValidation($field)); |
||||
|
0 ignored issues
–
show
It seems like
$entity->getMediaFieldsValidation($field) can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $rules of Illuminate\Support\Facades\Validator::make() does only seem to accept 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
Loading history...
|
|||||
| 93 | |||||
| 94 | if ($validator->fails()) { |
||||
| 95 | return redirect()->back()->withErrors($validator)->withInput(); |
||||
|
0 ignored issues
–
show
The function
redirect 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
Loading history...
|
|||||
| 96 | } |
||||
| 97 | |||||
| 98 | if ($request->hasFile($field)) { |
||||
| 99 | $entity->getMedia($field)->each(function ($e) { |
||||
| 100 | $e->delete(); |
||||
| 101 | }); |
||||
| 102 | |||||
| 103 | $this->add($entity, $request->file($field), $field); |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | $deletedSuffix = config('medialibrary-extension.field_suffixes.deleted', '_deleted'); |
||||
|
0 ignored issues
–
show
The function
config 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
Loading history...
|
|||||
| 107 | if ($id = $request->get($field . $deletedSuffix)) { |
||||
| 108 | $entity->deleteMedia($id); |
||||
| 109 | } |
||||
| 110 | } |
||||
| 111 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths