Passed
Push — master ( 19fdc3...a3afa9 )
by Ben
07:24
created

MediagalleryController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 24
c 1
b 0
f 0
dl 0
loc 42
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A index() 0 40 4
1
<?php
2
3
namespace Thinktomorrow\Chief\Mediagallery\Http;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\DB;
7
use Thinktomorrow\AssetLibrary\Asset;
8
use Thinktomorrow\Chief\Urls\UrlHelper;
9
use Illuminate\Database\Eloquent\Builder;
10
use Thinktomorrow\Chief\App\Http\Controllers\Controller;
11
use Thinktomorrow\Chief\FlatReferences\FlatReferenceFactory;
12
13
class MediagalleryController extends Controller
14
{
15
    public function index(Request $request)
16
    {
17
        $search = $request->input('search', false);
18
        $unused = $request->input('unused', false);
19
        $owner = $request->input('owner', false);
20
21
        $assets = Asset::with('media')->orderBy('created_at', 'DESC')->select('assets.*');
22
23
        if ($search) {
24
            $assets->whereHas('media', function (Builder $query) use ($search) {
25
                $query->where('file_name', 'LIKE', '%' . $search . '%');
26
            });
27
        }
28
29
        if ($unused) {
30
            $assets->whereNotExists(function ($query) {
31
                $query->select(DB::raw(1))
32
                    ->from('asset_pivots')
33
                    ->whereRaw('asset_pivots.asset_id = assets.id')
34
                    ->where('asset_pivots.unused', 0);
35
            });
36
        }
37
38
        if($owner){
39
            $owner = FlatReferenceFactory::fromString($owner)->instance();
40
            $modelAssets = collect();
41
            $modelAssets = $modelAssets->merge($owner->assets());
42
43
            $owner->children()->each(function($module) use($modelAssets){
44
                $modelAssets = $modelAssets->merge($module->assets());
0 ignored issues
show
Unused Code introduced by
The assignment to $modelAssets is dead and can be removed.
Loading history...
45
            });
46
47
            $assets->whereIn('id', $modelAssets->pluck('id'));
48
        }
49
50
        $assets = $assets->paginate(20);
51
52
        $pages = UrlHelper::allOnlineModels();
53
54
        return view('chief::back.mediagallery.index', compact('assets', 'pages'));
55
    }
56
}
57