MediaLibraryController::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Back\Api;
4
5
use Response;
6
use Exception;
7
use Illuminate\Http\Request;
8
use Spatie\MediaLibrary\Media;
9
use Illuminate\Http\UploadedFile;
10
use Illuminate\Support\Collection;
11
use App\Http\Controllers\Controller;
12
use App\Http\Requests\Back\AddMediaRequest;
13
use Spatie\Blender\Model\Transformers\MediaTransformer;
14
15
class MediaLibraryController extends Controller
16
{
17
    public function add(AddMediaRequest $request)
18
    {
19
        $model = $this->getModelFromRequest($request);
20
21
        $files = $request->file('file');
22
23
        if (! is_array($files)) {
24
            $files = [$files];
25
        }
26
27
        $media = collect($files)
28
            ->map(function (UploadedFile $file) use ($model, $request) {
29
                return $model
30
                    ->addMedia($file)
31
                    ->withCustomProperties(['draft' => $request->has('redactor') ? false : true])
32
                    ->toCollection($request->get('collection_name', 'default'));
33
            });
34
35
        if ($request->has('redactor')) {
36
            return Response::json(['filelink' => $media->first()->getUrl('redactor')]);
37
        }
38
39
        return fractal()->collection($media)->transformWith(new MediaTransformer());
0 ignored issues
show
Documentation introduced by
new \Spatie\Blender\Mode...mers\MediaTransformer() is of type object<Spatie\Blender\Mo...rmers\MediaTransformer>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
    }
41
42
    public function index(Request $request)
43
    {
44
        $model = $this->getModelFromRequest($request);
45
46
        $collectionName = $request->get('collectionName');
47
48
        $media = $model->getMedia($collectionName)->reduce(function (Collection $collection, Media $media) {
49
            return $collection->push([
50
                'thumb' => $media->getUrl('admin'),
51
                'image' => $media->getUrl('redactor'),
52
                'name' => $media->name,
53
            ]);
54
        }, new Collection());
55
56
        return response()->json($media);
57
    }
58
59
    protected function getModelFromRequest(Request $request)
60
    {
61
        if (! isset($request['model_name'])) {
62
            throw new Exception('No model name provided');
63
        }
64
65
        if (! isset($request['model_id'])) {
66
            throw new Exception('No model id provided');
67
        }
68
69
        return call_user_func($request['model_name'].'::findOrFail', $request['model_id']);
70
    }
71
}
72