Completed
Pull Request — master (#45)
by Sebastian
03:52
created

MediaLibraryApiController::index()   A

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;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\Back\AddMediaRequest;
7
use Exception;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Collection;
10
use Spatie\MediaLibrary\Media;
11
use App\Models\Transformers\MediaTransformer;
12
13
class MediaLibraryApiController extends Controller
14
{
15
    public function add(AddMediaRequest $request)
16
    {
17
        try {
18
19
            $model = $this->getModelFromRequest($request);
20
21
            $media = $model
22
                ->addMedia($request->file('file')[0])
23
                ->withCustomProperties(['temp' => $request->has('redactor') ? false : true])
24
                ->toCollection($request->get('collection_name', 'default'));
25
26
            if ($request->has('redactor')) {
27
                return Response::json(['filelink' => $media->getUrl('redactor')]);
28
            }
29
30
            return response()->json(
31
                fractal()
32
                    ->item($media)
33
                    ->transformWith(new MediaTransformer())
0 ignored issues
show
Documentation introduced by
new \App\Models\Transformers\MediaTransformer() is of type object<App\Models\Transformers\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...
34
                    ->toArray()
35
            );
36
37
        } catch (Exception $e) {
38
39
            return response(null, 500);
40
41
        }
42
    }
43
44
    public function index(Request $request)
45
    {
46
        $model = $this->getModelFromRequest($request);
47
48
        $collectionName = $request->get('collectionName');
49
50
        $media = $model->getMedia($collectionName)->reduce(function (Collection $collection, Media $media) {
51
            return $collection->push([
52
                'thumb' => $media->getUrl('admin'),
53
                'image' => $media->getUrl('redactor'),
54
                'name' => $media->name,
55
            ]);
56
        }, new Collection());
57
58
        return response()->json($media);
59
    }
60
61
    protected function getModelFromRequest(Request $request)
62
    {
63
        if (!isset($request['model_name'])) {
64
            throw new Exception('No model name provided');
65
        }
66
67
        if (!isset($request['model_id'])) {
68
            throw new Exception('No model id provided');
69
        }
70
71
        return call_user_func($request['model_name'].'::findOrFail', $request['model_id']);
72
    }
73
}
74