MediaGalleryController   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A index() 0 33 2
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Http\Controllers\Api;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Http\Request;
7
use Thinktomorrow\AssetLibrary\Asset;
8
use Thinktomorrow\Chief\App\Http\Controllers\Controller;
9
10
class MediaGalleryController extends Controller
11
{
12
    public function index(Request $request)
13
    {
14
        $limit = 9;
15
        $offset = 0;
16
        $excluded = [];
17
18
        $limit = $request->query()['limit'] ?? $limit;
19
        $offset = $request->query()['offset'] ?? $offset;
20
        $search = $request->query()['search'] ?? '';
21
        $conversion = $request->query()['conversion'] ?? 'full';
22
23
        $excluded = isset($request->query()['excluded']) ? explode(",", $request->query()['excluded']) : $excluded;
24
25
        $links = Asset::with('media')
26
            ->orderBy('created_at', 'DESC')
27
            ->whereNotIn('id', $excluded)
28
            ->whereHas('media', function (Builder $query) use ($search) {
29
                $query->where('mime_type', 'LIKE', '%image%');
30
                $query->where('name', 'LIKE', '%' . $search . '%');
31
            })
32
            ->offset($offset)
33
            ->limit($limit)
34
            ->get()
35
            ->map(function ($asset) use ($conversion) {
36
                return [
37
                    "id" => $asset->id,
38
                    "url" => $asset->url($conversion),
39
                    "filename" => $asset->filename(),
40
                    "size" => $asset->getSize(),
41
                ];
42
            });
43
44
        return response()->json($links->all());
45
    }
46
}
47